Heading Here

Please help me. When I get the value of price and stock whatever number value I input will turn into 0.00 while the stock turns empty.Btw I can get the other fields the code and the title values my only problem are the stock and pice.

In the sql of php myadmin i declared the title and code as varchar while price is decimal and stock is integer.
What do I need to do with the price and stock please help meeeeeee....thank you=)

    $getcode= $_GET['code'];
    $gettitle=$GET_['title'];
    $getprice=$GET_['price'];   
    $getstock=$GET_['stock'];




echo"

<table>
<tr><td><input type=text name=code[]>Code</tr></td>
<tr><td><input type=text name=title[]>Title</tr></td>
<tr><td><input type=number name=price[]>Price</tr></td>
<tr><td><input type=number name=stock[]>Stock</tr></td>

<tr><td><input type=submit value=ADD name=adds ></tr></td>
</table>

";
}

if (isset ($_GET['adds'])){

     mysql_connect("localhost", "root", "")or die("Unable to Connect". mysql_error());
     mysql_select_db("dbonlinebookstore");

     mysql_query("INSERT INTO books VALUES('$getcode[0]','$gettitle[0]','$getprice','$getstock')");

      $booksres=mysql_query("SELECT * FROM books");
      echo  "Book has been added"   ; 



while($row = mysql_fetch_array($booksres))

{

echo"<table><tr>";
echo "<td>" . $row['Code'] . "</td>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Stock'] . "</td>";
echo "</tr>";

echo"</table>";

}
}

just checking but these should all be $_GET?

$getcode= $_GET['code'];
$gettitle=$GET_['title'];
$getprice=$GET_['price'];   
$getstock=$GET_['stock'];

When you use <input type=text name=code[]> on an input it makes the variable an array, if you are just passing 1 value don't bother just do this:

<tr><td><input type=text name=code>Code</tr></td>
<tr><td><input type=text name=title>Title</tr></td>
<tr><td><input type=number name=price>Price</tr></td>
<tr><td><input type=number name=stock>Stock</tr></td>

The reason its not working is in your mysql query:

mysql_query("INSERT INTO books VALUES('$getcode[0]','$gettitle[0]','$getprice','$getstock')");

You have declared them all as arrays, so $getcode[0] and $gettitle[0] will work, and $getprice and $getstock will fail cause they are all arrays with 1 entry.

Either fix the input or update $getprice and $getstock to $getprice[0] and $getstock[0]

Hi,

Replace the line in your code with:

$getcode = $_GET['code'][0];
$gettitle = $_GET['title'][0];
$getprice = $_GET['price'][0];
$getstock = $_GET['stock'][0];

i did the repalacement and i recieve this warning

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\xampp\onlinebookstore\admin.php on line 51

Hi dear,

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\xampp\onlinebookstore\admin.php on line 51

these type of error appear when you miss any parameter or null value.

if you provide me you mysql_fetch_array() query with data base table with data . then i can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.