...

View Full Version : PHP/SQL Insert not working



tjkuster
03-18-2009, 03:55 PM
Any ideas why the following image insert coding is not working, but giving me a success statement?

States that the id is 0 everytime...



<html>
<head><title>Store binary data into SQL Database</title></head>
<body>

<?php
// code that will be executed if the form has been submitted:

if ($submit) {

// connect to the database
// (you may have to adjust the hostname,username or password)

MYSQL_CONNECT("10.8.11.72","CommBizGuide","Abcd1234");
mysql_select_db("free_listing");

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

$result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
"VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";

MYSQL_CLOSE();

} else {

// else show the form to submit new data:
?>

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>
File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="submit" value="submit">
</form>

<?php

}

?>

</body>
</html>


Thanks

abduraooft
03-18-2009, 04:00 PM
Where is your update query? Also read and follow http://www.codingforums.com/postguide.htm, especially the one about cross/duplicate posting (I think, it's a follow up of your other thread (http://www.codingforums.com/showthread.php?p=794416) ).

tomws
03-18-2009, 05:34 PM
From the man page for mysql_insert_id() (http://us.php.net/mysql_insert_id):



Return Values

The ID generated for an AUTO_INCREMENT column by the previous INSERT query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.


So, you're missing an auto-increment field, or perhaps your query fails. Since you're not error-checking return values from the functions, who knows?

sea4me
03-19-2009, 02:43 AM
Where are '$form_description','$data','$form_data_name','$form_data_size','$form_data_type' set???

If they are set, try this:

<html>
<head><title>Store binary data into SQL Database</title></head>
<body>

<?php
// code that will be executed if the form has been submitted:

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

// connect to the database
// (you may have to adjust the hostname,username or password)

MYSQL_CONNECT("10.8.11.72","CommBizGuide","Abcd1234");
mysql_select_db("free_listing");

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

$query = "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) VALUES ('".$form_description."','".$data."','".$form_data_name."','".$form_data_size."','".$form_data_type."')";
$sql = mysql_query($query) or die(mysql_error());

$id= mysql_insert_id();
echo "<p>This file has the following Database ID: <b>".$id."</b>";

MYSQL_CLOSE();

} else {

// else show the form to submit new data:
?>

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>
File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="submit" value="submit">
</form>

<?php

}

?>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum