PDA

View Full Version : What's wrong with this insert query?


robdog119
05-19-2006, 05:29 AM
I have a fairly simple insert query that keeps inserting blank records into my database. Here is the php, but let me know if it's not the code but perhaps a problem with something else on the page. Also, does anyone know how to get the line "Artist Added Successfully" to show on the page only if the insert was actually successful. Thanks!

if(isset($_POST['submit']))
{
$eventID = '';
$first = (isset($_POST['header'])) ? (addslashes($_POST['header'])) : false;
$last = (isset($_POST['specbody'])) ? (addslashes($_POST['specbody'])) : false;

$query= "INSERT INTO `specevent` (eventID,
header,
specbody)
VALUES ('',
'$header',
'$specbody')";
mysql_query($query) or die(mysql_error());
//echo "Artist Added Successfully";
}

Brandoe85
05-19-2006, 05:33 AM
Rob,

Your variable names are off, you're setting $first and $last, but inserting $header and $specbody.

As for your second question:

if(isset($_POST['submit']))
{
$eventID = '';
$header= (isset($_POST['header'])) ? (addslashes($_POST['header'])) : false;
$specbody= (isset($_POST['specbody'])) ? (addslashes($_POST['specbody'])) : false;

$query= "INSERT INTO `specevent` (eventID,
header,
specbody)
VALUES ('',
'$header',
'$specbody')";
$result = mysql_query($query) or die(mysql_error());
if($result)
{
echo "Artist Added Successfully";
}
}


Good luck;

robdog119
05-19-2006, 05:22 PM
Oh man, I can't believe I didn't see that! Guess I've been staring at code for too many days in a row. And I was really wracking my brain because I was comparing it to something else that did work. I guess I know now to go back and check the basics, not to mention, the error should have tipped me off. Thanks Brando!

If I can just add another question on to this: I have a page that updates this same table in the db, but it also does not work properly. This one might not be quite as simple. This page (specEventEdit.php) displays all of the records for updating. Then, the user clicks submit and it should update each record based on it's eventID. Right now, it doesn't seem to be updating anything. Here is the code thus far, but I think it might be that I need to add something to the page with the form. The following code is on a separate page which the form goes to (specEventUpdate.php):
$eventID = '';
$header = addslashes($_POST['header']);
$specbody = addslashes($_POST['specbody']);

//include necessary files
include("../redbenlogin.inc");

$query = ("UPDATE IGNORE `specevent` SET eventID ='$eventID',
header ='$header',
specbody ='$specbody'
WHERE eventID = '$eventID'");

mysql_query($query) or die(mysql_error());
echo "Special Event Page Update Completed";
Thanks!

Brandoe85
05-19-2006, 09:22 PM
You're setting $eventID to nothing, but you surely need it in your where clause. So i'm not sure what method you're using for the form, but just like before, either $_GET or $_POST. You also shouldn't be updating the eventID in your db. It doesn't make much sense.
A good thing to do is to echo our your sql statements and see everything that it's trying to do. It'll tell ya alot about whats going on.

Good luck;

robdog119
05-24-2006, 03:41 PM
Awesome, got it working well now. Yea, I didn't mean to be updating the eventID in the DB, you're right that it doesn't make much sense. Also, I did echo out the sql statements and that helped me figure out what was going on. Thanks for all your help on this!