zainab .a.k zainab .a.k - 7 months ago 16
PHP Question

PHP could not execute query

i am using php 5.6.20

in the admin page i am trying to Add product but it seems like the post dose not work and the query dose not executed
The database taple
The Form

<form id="myform"method="post"action="Admin.php">

<table class="userinf">
<tr>
<th colspan="2"><label class="labeli">Add Product</label></th>
</tr>
<tr>
<td><label class="labelii">PID</label></td>
<td><input id ="iitxt0" class ="itxt"type="text" name="PID" value="<?php echo $row["UID"]; ?>"></td>
</tr>
<tr>
<td><label class="labelii">Name</label></td>
<td><input id ="iitxt1" class ="itxt"type="text" name="Name" required value="<?php echo $row["Name"]; ?>" "></td>
</tr>
<tr>
<td><label class="labelii">Picture</label></td>
<td><input id ="iitxt2" class ="itxt" type="text" name="Picture" required value="<?php echo $row["Email"]; ?>"></td>
</tr>
<tr>
<td><label class="labelii">Price</label></td>
<td><input id ="iitxt3" class ="itxt" type = "text" name="Price" pattern="[-+]?([0-9]*\.[0-9]+|[0-9]+)" required value="<?php echo $row["PhoneNo"]; ?>"></td>
</tr>
<tr>
<td><label class="labelii">Type</label></td>
<td><input id ="iitxt4" class ="itxt" type="text" name="Type" required value="<?php echo $row["CreditNo"]; ?>"></td>
</tr>
<tr>
<td><label class="labelii">Stock</label></td>
<td><input id ="iitxt5" class ="itxt"type="number" name="Stock" value="<?php echo $row["City"]; ?>"></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" id="Sbutt" class="Ebutton" onclick="submitform()">Save</button>
</td>
</tr>
</table>
</form>


The PHP Code

<?php

var_dump();
if (isset($_POST["PID"]) && isset($_POST["Name"]) && isset($_POST["Picture"]) && isset($_POST["Price"]) && isset($_POST["Type"]) && isset($_POST["Stock"])){

$PID=$_POST['PID'];
$Name=$_POST['Name'];
$Picture=$_POST['Picture'];
$Price=floatval($_POST['Price']);
$Type=$_POST['Type'];
$Stock=intval($_POST['Stock']);
$query="INSERT INTO `product` (`PID`, `Name`, `Picture`, `Price`, `Type`, `Stock`)"+
+"VALUES ('$PID', '$Name', '$Picture','$Price', '$Type', '$Stock')";
}
if($_POST){
if(!($database=mysql_connect("localhost","wadiahS","123456")))
die("Could not connect to database</body></html>");
if(!mysql_select_db("glow",$database))
die("Could not open database</body></html>");
if(!($result=mysql_query($query,$database))){
print("<p>could not execute query</p>");
die(mysql_error()."</body></html>");
}
mysql_close($database);
}
?>


And when i try to post
ii get the error
///
Warning: var_dump() expects at least 1 parameter, 0 given in C:\xampp\htdocs\web\pages\Admin.php on line 294
could not execute query

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0' at line 1
///

Answer

You are concatenating the query using plus(+) In PHP Strings are concatenating using dot(.) Also either remove the var_dump() or add any parameter to it.

$query="INSERT INTO `product` (`PID`, `Name`, `Picture`, `Price`, `Type`, `Stock`)" . "VALUES ('$PID', '$Name', '$Picture','$Price', '$Type', '$Stock')";