Jump to content

[SOLVED] mysql_real_escape_string with POST


coutts

Recommended Posts

Hi can anyone tell me why the following code fails ie doesnt work, obviously I have a syntax problem but I have limited knowledge to fix it - I do BTW have an active connection by this stage of the code

 

Thanks

Robert

 

 

$sql="INSERT INTO my_table (ID, username, approved, composition) VALUES ('',mysql_real_escape_string($_POST['username']),mysql_real_escape_string($_POST['approved']),mysql_real_escape_string($_POST['composition']))";

Link to comment
Share on other sites

This did work until I started adding the mysql_real_escape_string even with the quotes although this reminds me on another problem that I was instructed not to use "" to make a variable out of aN SQL string and then try to run it. What would you suggest.

Link to comment
Share on other sites

(at least) Two solutions are possible:

 

1st

$username = mysql_real_escape_string($_POST['username']);
$approved = mysql_real_escape_string($_POST['approved']);
$composition = mysql_real_escape_string($_POST['composition']);
$sql="INSERT INTO my_table (ID, username, approved, composition) VALUES ('','$username','$approved','$composition')";

 

2nd

$sql="INSERT INTO my_table (ID, username, approved, composition) VALUES ('','".mysql_real_escape_string($_POST['username'])."','".mysql_real_escape_string($_POST['approved'])."','".mysql_real_escape_string($_POST['composition'])."')";

 

In general, you cannot do someting like this:

$var = "mysql_real_escape_string($argument)";

Only variables are recognized within "" quotes. Functions are not.

 

 

Link to comment
Share on other sites

I used example #1 as I did have a page made up like that which hadnt been working either - but I could see from your example what the problem was.

As a VB programmer this is quite a switch to using server side languages such as PHP and SQL database - VB is simpler

 

Thanks for your help

Rob

Link to comment
Share on other sites

I think if you start programming using VB its simpler - if you start with any other language it isnt. I think most languages share some of the same syntax and basic structure - VB is totally different so I dont think it is a good starter language

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.