1

I'm trying to edit the value and/or add more values to table options using PHP, here is a screen shot:


table name is


I started of with the following, now Im trying to see how I can pull the data that's associated with form_field_id, in this example is 5.

<?php

require_once("config/database.php");

$con = mysql_connect($config["db_server"],$config["db_user"],$config["db_pass"]);
mysql_select_db($config['db_name'], $con);

// get value of id that sent from address bar
$id=$_GET['form_field_id'];

// Retrieve data from database
$sql="SELECT * FROM options WHERE id='$form_field_id'";    


$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

?>  


<form name="form1" method="post" action="updated_values.php">

<input name="name" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">
<input name="name" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">
<input name="name" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">
<input name="name" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">
<input name="name" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">
<input name="name" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">

<button type="submit" /> Update </button>
</form>

Here is the updated code I added after your suggestion:

<?php
require_once("config/database.php");


$con = mysql_connect($config["db_server"],$config["db_user"],$config["db_pass"]);
mysql_select_db($config['db_name'], $con);


// get value of id that sent from address bar
$id=$_GET['form_field_id'];

// Retrieve data from database
$sql="SELECT * FROM options WHERE id='$id'"; 


$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

?>



<form name="form1" method="post" action="updated_values.php">

<?php
while($rows=mysql_fetch_array($result))
{
?>
   <input name="name[]" class="form-control" type="text" id="name" value="<? echo $rows['value']; ?>">
<?php } ?>

<button type="submit" /> Update </button>
</form>
piet.t
  • 11,718
  • 21
  • 43
  • 52
user2958236
  • 68
  • 1
  • 8
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Feb 23 '14 at 22:51

2 Answers2

1

You have an error in your query, you are passing a variable thta you didn't assign so change first query as follow

$sql="SELECT * FROM options WHERE id='$id'"; 
                                   //^here you used a wrong variable

You also need to loop throw your records to print all them, so change as follow

<form name="form1" method="post" action="updated_values.php">

<?php
while($rows=mysql_fetch_array($result))
{
?>
   <input name="name[]" class="form-control" type="text" id="name[]" value="<? echo $rows['value']; ?>">
<?php } ?>

<button type="submit" /> Update </button>
</form>

Note that i also changed name of your input and i added [] so you will have an array of name input. As side note i'd say your code is highly vulnerable to mysql injection and you should switch either to mysqli or PDO and use prepared statments to avoid any problems.

Fabio
  • 23,183
  • 12
  • 55
  • 64
  • Thanks, still bit confused, how to pull the data using values.php?form_field_id=5 will that load the value into input files? – user2958236 Feb 23 '14 at 23:04
  • You did correctly with `$id=$_GET['form_field_id'];` and then use query i placed in my answer – Fabio Feb 23 '14 at 23:06
  • please see the updated codes above with the changes with your suggestion, but when I goto url values.php?form_field_id=5 nothing shows, beside the update button – user2958236 Feb 23 '14 at 23:25
0

Your code looks right. You just need to change this:

$sql="SELECT * FROM options WHERE id='$form_field_id'"; 

to this:

$sql="SELECT * FROM options WHERE form_field_id='$id'";

All you had wrong was the variable and the column name, and to show the results you have to properly loop through all the rows you get.

Bruno Fondevila
  • 159
  • 1
  • 2