Jump to content

Small Syntax Issue


bobicles2

Recommended Posts

The code below after Inserting into my table echos either "error:" or "Thank you.." where have i gone wrong here? the intention was to echo an error if there was one...otherwise if it works fine and adds to the table echo "Thanks etc"

 

$sql="INSERT INTO Events (Event, Genre, Date, Price, Venue, Tickets, Postcode)
      VALUES ('$event','$genre','$date','$price','$venue','$tickets','$postcode')";
$result = mysql_query($sql, $con);

if ($result)
{
    echo "Error :" . mysql_error();
}
else
{
    echo "Thank You, Your Event has now been added to our Records";
}

mysql_close($con);

Link to comment
https://forums.phpfreaks.com/topic/199410-small-syntax-issue/
Share on other sites

First, I know this is handy for debugging, but don't echo errors to the end users, they don't need to know your table's structure.  Second, testing if ($result) is not the correct way to do this:

 

try:

$sql="INSERT INTO Events (Event, Genre, Date, Price, Venue, Tickets, Postcode)
      VALUES ('$event','$genre','$date','$price','$venue','$tickets','$postcode')";
if (!mysql_query($sql, $con))
{
echo "error!";
}
else
{
echo "Thank you";
}

 

Really the only thing you had wrong is they were backwards.

 

Link to comment
https://forums.phpfreaks.com/topic/199410-small-syntax-issue/#findComment-1046596
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.