Forum Moderators: open

Message Too Old, No Replies

MySQL error

pls need help

         

eK3eKyToPa

9:21 am on Jan 25, 2008 (gmt 0)

10+ Year Member



Please see the code below and say where is my mistake

if(isset($_POST['submit'])) {

$name = mysql_real_escape_string($_POST[name]);
$query = "SELECT * FROM groups WHERE group_name='$name'";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
$import = mysql_query("INSERT INTO groups (ip, group_name, group_atr, group_info) VALUES ('$ip', '$_POST[name]', '$_POST[atributes]', '$_POST[group_info]')");
print '<div align="left">Successfully added</div>';
} else
$import = mysql_query("INSERT INTO groups (ip, group_name, group_atr, group_info) VALUES ('$ip', '$_POST[name]', '$_POST[atributes]', '$_POST[group_info]')");
print '<div align="left">Successfully updated</div>';
}

}


This after clicking the submit button, must check if the name exists in the DB and if not, is added a new row with its information , if the name exists it updates the information about the name

Frank_Rizzo

9:57 am on Jan 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Error messages would help!

$name = mysql_real_escape_string($_POST[name])

This line should be

$name = mysql_real_escape_string($_POST['name'])

Note the single quotes around 'name'

You would also need to do the same with your insert query

$import = mysql_query("INSERT INTO groups (ip, group_name, group_atr, group_info) VALUES ('$ip', '$_POST[name]', '$_POST[atributes]', '$_POST[group_info]')");
print '<div align="left">Successfully added</div>';

But I guess you tried that and got errors due to mis-matching of single quotes.

Try this instead:

***************************
if(isset($_POST['submit'])) {

$name = mysql_real_escape_string($_POST['name']);
$atributes = mysql_real_escape_string($_POST['attributes']);
$group_info = mysql_real_escape_string($_POST['group_info']);

$query = "SELECT * FROM groups WHERE group_name='$name'";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
$import = mysql_query("INSERT INTO groups (ip, group_name, group_atr, group_info) VALUES ('$ip', '$name', '$atributes', '$group_info')");
print '<div align="left">Successfully added</div>';
} else
$import = mysql_query("INSERT INTO groups (ip, group_name, group_atr, group_info) VALUES ('$ip', '$name', '$atributes', '$group_info')");
print '<div align="left">Successfully updated</div>';
}

}
***************************

To be more secure you should FILTER_SANITIZE_STRING as this will block more harmful stuff which someone may try to compromise your system.

eK3eKyToPa

8:12 pm on Jan 25, 2008 (gmt 0)

10+ Year Member



now it works
Thanks you!
I have missed and { after the } else