简体   繁体   中英

PHP won't insert into the database in PHPmyAdmin

So I've been battling with this for days trying to figure out why my 'products' are not uploading into phpMyAdmin after the user submits a form - The application is one that allows members to create their own products...& the place i'm stuck at is as follows: I'm a logged in user i'm on the "List a Product" page from my user admin. when i enter the product info then click to submit it won't post the product & associated data to phpmyadmin. and I'm trying to move the image uploaded to a folder via a move_uploaded_file function. Any help would be greatly appreciated. All code should be below (database is connected and all the sessions set properly, etc I believe):

Form from the create_product.php (the page where a logged in user lists a new product:

if (isset($_POST['product_name'], $_POST['product_description'])) {
$product_name = $_POST['product_name'];
$product_description = $_POST['product_description'];

$errors = array();

if (empty($product_name) || empty($product_description)) {
    $errors[] = "All Fields are required";
} else {
    if (strlen($product_name) > 55 || strlen($product_description) > 255) {
        $errors[] = "One or more fields contains too many characters";
    } 

}

if (!empty($errors)) {
    foreach ($errors as $error) {
        echo $error, '<br/>';
        }
    } else {
        create_product($product_name, $product_description);

        header("location: product.php?product_id='.$product_id.'");
        exit();
    }
}

<form action="" method="post" enctype="multipart/form-data">
        <p>Product Name:<br/><input type="text" name="product_name" maxlength="55" /></p>
        <p>Describe this Product:<br/><textarea name="product_description" rows="6" cols="35" maxlength="255"></textarea></p>

        <p>Upload a product profile pic:<br/><input type="file" name="fileField" id="fileField" /></p>
        <p><input type="submit" value="Create"></p>
    </form>

Function from my product.functions.php file (which is included on the create_product.php file):

function create_product($product_name, $product_description) {
$product_name = mysql_real_escape_string(htmlentities($product_name));
$product_description = mysql_real_escape_string(htmlentities($product_description));

mysql_query("INSERT INTO `products` VALUES ('','".$_SESSION['member_id']."', UNIX_TIMESTAMP(), '$product_name', '$product_description')");

$bmid = mysql_insert_id();
// place image in the folder
$newname = "$bmid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],'uploads/$newname');
}

I believe my issue may be coming from the image upload, but i've been struggling with this forever...Thanks for any help you can provide...

There is a few issues with this. First is the insert statement. You do not need the `, that is MySQL specific and not required. Second you have a null value for what might be your ID field. change your insert statement to be (of course with your column names):

 INSERT INTO products (member,date_time,prod_name,prod_desc) VALUES ('".$_SESSION['member_id']."', UNIX_TIMESTAMP(), '$product_name', '$product_description')

That woud cause your file not to upload and the database not to be updated. The other thing that you can try and can cause some issues is removing the php variables from the string and concating them together. The newname variable should be written as.

$location = "uploads/" . $bmid . ".jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],$location);

If this does not work then you will need to output the SQL and any error that it is throughing.

$SQL = "INSERT INTO products (member,date_time,prod_name,prod_desc) VALUES ('".$_SESSION['member_id']."', UNIX_TIMESTAMP(), '".$product_name."', '".$product_description".')";
$result = mysql_query($SQL);
if(!$result) echo "SQL: ", $SQL, "<br />ERROR: ", mysql_error();

问题出在您的INSERT查询中,您忘记将列名放在VALUES()之前,它应该是INSERT INTO table_name (columns_name) VALUES (values);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM