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
Share on other sites

$queryreg = mysql_query("INSERT INTO `stock` . `stocks` (`id`, `stockname`, `stockqty`) VALUES (NULL,'$stockname','$stockqty')");

 

how about try this:

 

$queryreg = mysql_query("INSERT INTO stocks(stockname, stockqty) VALUES ('$stockname','$stockqty')");

 

Link to comment
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!");
	}

Link to comment
Share on other sites

ive checked to see if it exists but for example when i got to add exactly the same stock name it just adds a new one rather than echoing an error message saying item already exists, please updata existing values.

 

Thanks

 

Lance

Link to comment
Share on other sites

$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
....................

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.