Jump to content

values wont add to db


lanceox

Recommended Posts

hi guys hope some1 can help. 

 

im creating a system that will allow stock to be added to a database and modifed.  However on the add section, nothing adds it only inputs a new id row in the database and the other fields are blank.  Can anyone have a look for me :)

This sections is the form handler once the user has input a stockname and stock qty

 

 

<?php
session_start();


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>stock</title>
</head>

<body>
<?php
$stockname = &$_POST['stockname'];
$stockqty = &$_POST['stockqty'];


	$connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!");
	mysql_select_db("stock", $connect) or die("Couldn't find db"); // select database

	$query = mysql_query("SELECT * FROM stocks WHERE stockname=$stockname, stockqty=$stockqty");
	$numrows = mysql_num_rows($query);


	if ($numrows!=0)
	{
		echo("Item already exists!  Try Updating the items instead!");	
	}
	else
	{
		$queryreg = mysql_query("INSERT INTO `stock` . `stocks` (`id`, `stockname`, `stockqty`) VALUES (NULL,'$stockname','$stockqty')");

		echo("stock added!");
	}


?>
<center><table>
<tr>
    	<td>
        
        Stock_id
        </td>
        <td>
        
        Stock_name
        </td>
        <td>
        
        Stock_Qty
        </td>
    </tr>
</table></center>
</body>
</html>

 

Thanks

 

 

Lance

Link to comment
https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/
Share on other sites

thanks it worked better than the way i done it.  Thanks :)

 

Is there a way i can check if stock exists then print message otherwise add to db.  This is the kind of thing i mean.... where am i going wrong ?

 


$connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!");
	mysql_select_db("stock", $connect) or die("Couldn't find db"); // select database

	$query = mysql_query("SELECT * FROM stocks WHERE stockname=$stockname, stockqty=$stockqty");
	$numrows = mysql_num_rows($query);


	if ($numrows!=0)
	{
		echo("Item already exists!  Try Updating the items instead!");	
	}
	else
	{
		$queryreg = mysql_query("INSERT INTO `stocks` (stockname, stockqty) VALUES ('$stockname','$stockqty')");

		echo("stock added!");
	}

$query = mysql_query("SELECT * FROM stocks WHERE stockname='$stockname'");
	$numrows = mysql_num_rows($query);


	if ($numrows>0)
	{
		mysql_query("UPDATE stocks SET stockqty='$stockqty' WHERE stockname = '$stockname'");
                       echo("Item already exists!  Its updated");	
	}
	else
....................

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.