ovyxdiu
ovyxdiu

Reputation: 9

PHP SELECT where

First of all sorry for my bad english.

I have a php script that display a list of products from sql:

$cerereSQL = 'SELECT * FROM `produse`'; 
   $rezultat = mysql_query($cerereSQL);
   while($rand = mysql_fetch_array($rezultat)){
echo '
<form action="editare.php" method="post">
<table width="100%" cellpadding="0" cellspace="0">
<tr style="text-align:center;" height="25">
<td style="border:1px solid black;" width="7%" height="10"><i> '.$rand['indice'].' </i></td>
<td style="border:1px solid black;" width="40%"><i> '.$rand['denumire_produs'].' </i></td>
<td style="border:1px solid black;" width="20%"><i> '.$rand['producator'].' </i></td>
<td style="border:1px solid black;" width="10%"><i> '.$rand['pret'].' </i></td>
<td style="border:1px solid black;" width="6%"><i> '.$rand['valuta'].' </i></td>
<td style="border:1px solid black;" ><i> '.$rand['cod'].' </i></td>
<td width="5%"><input type="submit" name="submit" value="Edit"></td>
</tr>
</table>
</form>';

I want the button (value="Edit") from the end of each row to select exactly the product from the same row, to be edited.

I tried next code but did't work (i have a list with 5000 lines):

switch($_GET['actiune'])
{
case '':

$cerereSQL = 'SELECT * FROM `produse` WHERE denumire_produs="'.$rand['denumire_produs'].'"'; 

I know i have to use: $cerereSQL = 'SELECT * FROMproduseWHERE denumire_produs="......"; but i don't know how to do this, how to make the edit button, open the exactly product from the same row. Can someone please help me with a suggestion?

Upvotes: 0

Views: 66

Answers (2)

M.chaudhry
M.chaudhry

Reputation: 651

Four Things

First You dun need single Quotes around table name

$cerereSQL = 'SELECT * FROM produse'; 

Second Dont use mysql it is deprecated unless you really need to do

third you need to do this while you are echo any thing like that method=\"POST\" your code will not execute due to that

four you need hidden field like that

<input type=\"hidden\" name=\"demuire\" value=\"'.$ran['whatever'].'\"/>
<td width="5%"><input type="submit" name="submit" value="Edit"></td>

then you can get this by simple

$_POST['demuire']

Upvotes: 1

Rob W
Rob W

Reputation: 9142

Perhaps you mean to do this:

$cerereSQL = 'SELECT * FROM `produse`'; 
   $rezultat = mysql_query($cerereSQL);
   while($rand = mysql_fetch_array($rezultat)){
echo '
<form action="editare.php" method="post">
<table width="100%" cellpadding="0" cellspace="0">
<tr style="text-align:center;" height="25">
<td style="border:1px solid black;" width="7%" height="10"><i> '.$rand['indice'].' </i></td>
<td style="border:1px solid black;" width="40%"><i> '.$rand['denumire_produs'].' </i></td>
<td style="border:1px solid black;" width="20%"><i> '.$rand['producator'].' </i></td>
<td style="border:1px solid black;" width="10%"><i> '.$rand['pret'].' </i></td>
<td style="border:1px solid black;" width="6%"><i> '.$rand['valuta'].' </i></td>
<td style="border:1px solid black;" ><i> '.$rand['cod'].' </i></td>
<td width="5%"><input type="hidden" name="denumire_produs" value="'.$rand['denumire_produs'].'"><input type="submit" name="submit" value="Edit"></td>
</tr>
</table>
</form>';

Then, snag the denumire_produs from $_POST:

$denumire_produs = intval($_POST['denumire_produs']); // is this an INTeger? Otherwise, use `mysql_real_escape_string` -- or use PDO prepared statements.
$cerereSQL = 'SELECT * FROM `produse` WHERE denumire_produs="'.$denumire_produs.'"'; 

Is this what you're trying to do?

Upvotes: 1

Related Questions