webdevRefinery Forum: Can someone check my SQL code? - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline renewaltopics 

  • Group: Members
  • Posts: 53
  • Joined: 25-March 12
  • Expertise:HTML,PHP,Javascript,SQL

Posted 14 July 2012 - 07:06 PM (#1)

Can someone check my SQL code?


There is a small error in my code but I dont know where...
Hi, I've created a database in the MySQL command line called "users". Ive then created a table in the users database called "users".
So heres the code i have on my .php file:

<?php

$con = mysql_connect("localhost","register","mysql") or die(mysql_error());
echo "Connected to MySQL<br />";

mysql_select_db("users, $con") or die("Could not find database");

mysql_query("INSERT INTO users (FirstName, LastName, Email, Password) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[password1]')");


mysql_close($con);
?>

Now heres the problem: When I submit to this page, the database isnt getting any data via "INSERT INTO" above. Is there some code im missing? or did i do everything wrong? thanks
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 14 July 2012 - 07:12 PM (#2)

most likely you need to escape a ' in your posted data. Use mysql_real_escape_stringuntil you decide to use PDO (like we, and PHP, advised).
Reserved.
0


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 14 July 2012 - 07:14 PM (#3)

The main problem I see is your lack of
[code][/code]
tags.
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
1


User is offline renewaltopics 

  • Group: Members
  • Posts: 53
  • Joined: 25-March 12
  • Expertise:HTML,PHP,Javascript,SQL

Posted 14 July 2012 - 07:25 PM (#4)

View PostTheEmpty, on 14 July 2012 - 07:12 PM, said:

most likely you need to escape a ' in your posted data. Use mysql_real_escape_stringuntil you decide to use PDO (like we, and PHP, advised).



May I ask what that real escape function does? and how to use it?
0


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 14 July 2012 - 07:55 PM (#5)

View Postrenewaltopics, on 14 July 2012 - 07:25 PM, said:

May I ask what that real escape function does? and how to use it?

Right now, you're letting the user add whatever they want to the database. That's a giant no-no. What
mysql_real_escape_string()
does is it purifies, or cleans (escapes) the strings that you're giving to the database so that your SQL queries can't easily be attacked. There are a number of other checks that you should do (but don't necessarily need to do) that would also help, but until you start using something like PDO (Kyek has a nice tutorial here) you're going to need to escape, or clean the strings yourself before inserting them into the database.

Edit: Also, since you are using mysql_, try adding
or die(mysql_error());
at the end of the
mysql_query()
line.
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is offline Renegade 

  • 418 I'm a teapot
  • Group: Members
  • Posts: 748
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL,Graphics

Posted 14 July 2012 - 08:18 PM (#6)

I'm not sure you can access the contents of an array in PHP directly from a string. Trying setting them in their own variables and then insert them or concating them using the dot operator.

Example:

$name = $_POST['name'];

echo "My name is $name"; //Notice the double quotes

//or
echo 'My name is ' . $_POST['name'];

http://adriancooney.ieGithubTwitterDribbbleForrst
We all die. The goal isn't to live forever. The goal is to create something that will.

Array(16).join({}-{}) + " Batman!";
0


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 14 July 2012 - 10:32 PM (#7)

I don't really respond to PMs for questions that could be asked publicly on the forum, but I will respond to your thread because I had the tab open ;-)

The way you're formatting your SQL string is really really bad. Not only would it be super easy for someone to attack that via SQL injection and dump all your data (see this post), the PHP involved in building that string is shaky. While you could add {} symbols to make sure those values resolve correctly, it's much more reliable to concatenate instead.

With that said, though, please please check out PDO and the concept of prepared queries via the link above. It takes maybe 10 minutes and will make your code more secure forever.

Once you fix that, though, if you're still having issues, just look at the error message :). If you're using PDO, just var_dump the output of this function: http://www.php.net/m....errorinfo.php. If you're not using PDO (which is bad -- again, take the 10 minutes for PDO) then this function will do it: http://www.php.net/m...mysql-error.php

Note, in that last link, the large red warning that you should be using PDO, directly from the guys who wrote PHP. Just in case my suggestion wasn't clear enough ;-).
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 14 July 2012 - 11:45 PM (#8)

First thing I saw:

mysql_select_db("users, $con")


0_o you sure?
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline Ruku 

  • I do Linux and that Internet thing.
  • Group: Members
  • Posts: 1351
  • Joined: 17-April 10
  • Location/root
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL

Posted 15 July 2012 - 04:46 AM (#9)

View PostRenegade, on 14 July 2012 - 08:18 PM, said:

I'm not sure you can access the contents of an array in PHP directly from a string. Trying setting them in their own variables and then insert them or concating them using the dot operator.


You can; you just have to wrap them in curly braces to help the interpreter:

echo "my name is {$_POST['name']}";

1


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 15 July 2012 - 07:36 AM (#10)

View Postcallumacrae, on 14 July 2012 - 11:45 PM, said:

First thing I saw:

mysql_select_db("users, $con")


0_o you sure?

Oh my, I totally missed that xD. That's why code tags and syntax highlighting are important!
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 15 July 2012 - 07:37 AM (#11)

Hey, I saw it without ;-)
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services