use checkbox id to retrieve value from mysql database and insert with php
Dear Stackoverflow users. First of all I love stackoverflow because there
is always someone willing to help. And I am hoping that someone has a
great idea to get the question below figured out.
I got the following checkboxes
<input type="checkbox" name="chk" id="10001" value="$row['prijs3']"
onclick="calculate()">
<input type="checkbox" name="chk" id="10002" value="$row['prijs3']"
onclick="calculate()">
<input type="checkbox" name="chk" id="10003" value="$row['prijs3']"
onclick="calculate()">
<input type="checkbox" name="chk" id="10004" value="$row['prijs3']"
onclick="calculate()">
The ids are productcodes. So 10001 is product 1. I have hundreds of these
checkboxes and each checkbox will gets his own id based upon the product
it represents. The code needs to lookup the id in the table and then
retrieve the third price(prijs3) and put this in the value area of the
checkbox.
I am trying the code below but am stuck on the retrieval part of the
productcode + i am not sure if the $row['prijs3'] part is working either:
<?
// Connect to database server
mysql_connect($server,$username,$password) or die (mysql_error ());
// Select database
mysql_select_db($database) or die(mysql_error());
// Product code retrieval
$procode =
// SQL query
$strSQL = "SELECT * FROM gypg8_artikelen WHERE productcode=$procode";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
}
?>
I have been searching google for an hour and above code was the best i
could come up with. Thanks for any help in advance.
The Question: how to use the checkbox id to do a lookup in a database
table and return a tablerow value in the value field of the checkbox.
Extra Question: is it also possible with the same code to show
$row['productname'] in an echo
A LOT OF THINGS HAS CHANGED SINCE THE BEGINNING OF THIS QUESTION BELOW ARE
THE UPDATED CODES
I switched to mysqli at least i think i have the right codes:
<?
// Connect to database server and database
mysqli_connect($server,$username,$password,$database);
// Product code retrieval
$procode = "SOMECODETHATPUTSHERETHECHECKBOXID"
// SQL query
$query = "SELECT * FROM `cypg8_artikelen` WHERE `productcode`=$procode";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
// output
while($row = $result->fetch_assoc()){
echo $row['productname'] . '<br />';
}
?>
The above code needs to be changed so it can collect the number in id=""
from the below checkboxes
<input type="checkbox" name="chk" id="10001" value="$row['prijs3']">
<input type="checkbox" name="chk" id="10002" value="$row['prijs3']">
<input type="checkbox" name="chk" id="10003" value="$row['prijs3']">
<input type="checkbox" name="chk" id="10004" value="$row['prijs3']">
And to make it even more difficult this needs to be done dynamically so i
dont have to copy the entire code for each checkbox.
No comments:
Post a Comment