-1

i am trying to insert details to the db. it runs a success message but doesnt insert any data to the db. tried different ways but still no data is inserted to the db.

i have updated my code to show the whole process. code:

     <?php 

 $con = mysqli_connect("localhost","root","","the_official_one");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


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

    $prod_name = strtoupper($_POST['prod_name']);
    $prod_brand = $_POST['prod_brand'];
    $prod_cat = $_POST['prod_model'];
    $prod_price = $_POST['prod_price'];
    $prod_desc = $_POST['prod_desc'];
    $prod_qty = $_POST['prod_qty'];
    $d = date("Y-m-d");

    mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date) 
    VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')");

    $uniq_id = mysqli_insert_id($con);
    echo $uniq_id." ";     



    foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_name){

    $name = $_FILES['product_image']['name'][$key];
    $tmpname = $_FILES['product_image']['tmp_name'][$key];
    $type = $_FILES['product_image']['type'][$key];
    $size = $_FILES['product_image']['size'][$key];        

    mkdir("product_images/$prod_name/");
    $dir = "product_images/$prod_name/";

    $move = move_uploaded_file($tmpname, $dir.$name);
    $path = "$prod_name/".$_FILES['product_image']['name'][$key];
    $img = $_FILES['product_image']['name'][$key];

    if($move) {                            
        $sql = mysqli_query($con,"INSERT into product_images(prod_id, image) VALUES ('".$uniq_id."', '".$img."')"); 
        $query = mysqli_query($con,"update products set product_image_1='$path' where product_title='$prod_name'");

    if($query) {
        header('location: add_prods.php?message=success');
    } else {
        header('location: add_prods.php?message=failed');
    }

    } else {
        echo '<hr>Picture upload failed<br /><hr />';
    }
    }  
   }    

   ?>

the code with error is:

      mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date) 
    VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')");
emilkitua
  • 95
  • 2
  • 3
  • 12

1 Answers1

1
mysqli_autocommit($con,TRUE);

mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date) 
VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')");

OR

mysqli_commit($con);

Either way you should think about closing the connection when you are done.

mysqli_close($con);

if neither of those work, try outputting the content of

mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date) 
VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')"

to see what is going wrong.

Either way Escape the variables if you havent before building that query to prevent SQL Injection.

Taff
  • 231
  • 1
  • 13