I Can´t get INSERT to work
I have been banging my head to the wall with this:
- get the POST values to array..
- Select the foreign key.. 开发者_运维技巧
- Some other INSERT-operations
$insertdates = "INSERT INTO dates (asid,acq_date, serv_guaranteedate , maintenance_period, expiration_date) VALUES ('$foreignkey','$uservalues[1]' ,'$uservalues[4]','$uservalues[5]','$uservalues[3]')"; $upsdasult= mysql_query($insertdates);
All the values are in the correct format before the insert and $foreignkey is in correct format. (I have echoed the values before the insert statement).
The operation runs succesfully, but when I look at the database, nothing has been inserted.Could Somebody please tell me what I´m doing wrong?
You have to use curly braces to embed a array variable:
echo "This is an {$array['value']}";
Use
$insertdates = "INSERT INTO dates (asid,acq_date, serv_guaranteedate,
maintenance_period, expiration_date)
VALUES ('".$foreignkey."','".$uservalues['1']."','".$uservalues['4'].
"','".$uservalues['5']."','".$uservalues['3']."')";
$upsdasult= mysql_query($insertdates);
If you do
$insertdates = "INSERT INTO dates (asid,acq_date, serv_guaranteedate
, maintenance_period, expiration_date)
VALUES ('$foreignkey','$uservalues[1]','$uservalues[4]'
,'$uservalues[5]','$uservalues[3]')";
$upsdasult= mysql_query($insertdates);
if (!$upsdasult)
{
die(mysql_errno($link) . ": " . mysql_error($link). "\n");
}
Chances are it doesn't like either the key, or a date format, but this will tell you.
check your $_POST values
try this query :
$query = sprintf("INSERT INTO dates set asid = '%s', acq_date = '%s', serv_guaranteedate = '%s', maintenance_period = '%s', expiration_date = '%s'", mysql_real_escape_string($foreignkey), mysql_real_escape_string($uservalues[1]), mysql_real_escape_string($uservalues[4]), mysql_real_escape_string($uservalues[5]), mysql_real_escape_string($uservalues[3]) );
$result = mysql_query($query);
if($result) { echo 'success'; } else { echo mysql_error(); }
Leave mysql_error() empty..... use the below code....
$upsdasult= mysql_query($insertdates) or die(mysql_error());
You dont need the rest of the code....
I think you have copied the same query from this page http://php.net/manual/en/function.mysql-error.php. But mysql_error will anyway print the error code for you....
Please try this and let me know if this is what you are looking for....
Is it not the case that your FK value should be an integer rather than a string? How about if you don't use '$foreignkey'
but only use instead $foreignkey
?
精彩评论