简体   繁体   中英

PHP and MySQL help needed

I have 2 tables in my database. categories and products. in categories there are 2 fields. catid and catname. and in products also there are 3 fields. id, catid and name.

in my submit form im fetching the catname in to a sector. what i wanna do is get value of the selector and save the catid in to products table catid field. instead of categories name. can anyone explain me how to do this. Thanks in advance.

Here is the code of submit form.

include("db.php");

$result = mysql_query("SELECT * FROM categories")
or die (mysql_error());

?>
<!--SubmitForm-->

<form method="post" action="add_products.php">
<select name="cat"> 
<?php
while($row = mysql_fetch_array($result))    
{echo "<option value='".$row[catid]."'>".$row[catname]."</option>";} 
?>
</select><br/>
<input type="text" name="name" value=""><br/>
<input type="submit" value="submit"/>
</form>

add_products.php Code

<?php
include("db.php");

$cat = $_POST['catid']; 

$query = "SELECT * FROM categories WHERE catname='$cat'";
$result= mysql_query($query) or die ('Mysql Error');

while($row = mysql_fetch_array($result)){
$catn = $row['catid'];

}


$name = mysql_real_escape_string($_POST['name']);
$query="INSERT INTO products(catid, name)VALUES ('".$catn."','".$name."')";
mysql_query($query) or die ('Error Updating');

echo "Product Added";


?>

Some unrelated, but very important things:

  • you should escape $cat before it goes into the query
  • you should always escape strings that go out to HTML with htmlspecialchars
  • you should always use $row['keyname'] , not the deprecated $row[keyname]

Now for your question. The code seems correct on first glance, but I don't have PHP right now so I can't test it. Is there anything in particular that is not working as expected?

You already have it in??

$cat = $_POST['catid'];

If you only want to insert IF they $cat exists, then:

<?php
include("db.php");

$cat = $_POST['catid']; 

$query = "SELECT * FROM categories WHERE catname='$cat'";
$result= mysql_query($query) or die ('Mysql Error');

if($result)
{
    $name = mysql_real_escape_string($_POST['store']);
    $query="INSERT INTO products(catid, name)VALUES ('".$catn."','".$name."')";
    mysql_query($query) or die ('Error Updating');
    echo "Product Added";
}
?>

You are already assigning the category ID to the category name in the select menu. The variable of the select menu is $_REQUEST['cat'] , which holds the ID of the selected category after submitting the form. You can save this value directly to the product table.

However, the while loop in add_products.php is of no use, since you are always assigning the last ID in the table to the variable $catn . Replace this while loop with $catn = $_REQUEST['cat'] (while cat is the name of the select menu).

seem many mistakes here:

select name="cat"

and your try to receive $cat = $_POST['catid']; the correct is $cat = $_POST['cat'];

then you tries to select by catname

$query = "SELECT * FROM categories WHERE catname='$cat'";

when you need to compare ids catid='$cat'";

and what for to assign meny times if the result is single?:

if ( ($row = mysql_fetch_array($result)) ){
  $catn = $row['catid'];

}

Your select field is names 'cat', so it should be $_POST['cat'] (or better, rename the select field to 'catid'). And it alreay contains the catid, so there's no need to get it from the DB again (unless you want to make sure it does in fact exist).

Finally, you should escape the $_POST['cat'] parameter as you do the name. So this is sufficient:

$catid = mysql_real_escape_string($_POST['cat']); 
$name  = mysql_real_escape_string($_POST['store']);

$query="INSERT INTO products(catid, name) VALUES ('".$catid"','".$name."')";
mysql_query($query) or die ('Error Updating');

echo "Product Added";

Please also look into PDO for the best way to handle DB queries like this.

try change this

"INSERT INTO products(catid, name)VALUES ('".$catn."','".$name."')";

to

"INSERT INTO products(catid, name)VALUES ('".$cat."','".$name."')";

You already seem to have the right values, just need to put them in the correct spot, if you need the 'catid', you can just put it in the id tag of the select.

When you echo the you just need to do this,

echo "<option id='".$row[catid]."' value='".$row[cat]."'>".$row[catname]."</option>";

For more info refer to the w3school manual for, at this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM