I'm trying to edit the value and/or add more values to table options using PHP, here is a screen shot:
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>
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.
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.
Related
I need to display only the id row of the database on a textbox, my problem was the data is displaying all id's rows and columns.
I was fetching all data in the database, but what I need is only the id on the database.
Here is my code in forms, input text fields and submit.
<form action="" method="POST">
<div class="row col-md-4">
<label>Amount</label>
<input type="text" name="id" class="form-control validate">
<br>
<input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input><br>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection, 'qrproject');
if(isset($_POST['search']))
{
$id = $_POST['id'];
$query = "SELECT * FROM scratch_cards WHERE amount='$id' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="" method="POST">
<input type="text" name="code" value="<?php echo $row['code'] ?>" class="form-control validate" id="mapo">
<input type="text" name="pin" value="<?php echo $row['pin'] ?>" class="form-control validate" id="mact">
<input type="text" name="status" value="<?php echo $row['status'] ?>" class="validate form-control" id="soluong">
<input type="date" name="card_expiration" value="<?php echo $row['card_expiration'] ?>" class="validate form-control" id="cardex">
<input type="number" name="card_validity" value="<?php echo $row['card_validity'] ?>" class="validate form-control" id="cardval">
</form>
<?php
}
}
?>
</form>
From your comments, it seems what you want to do is output a single, random row which matches the $_POST['id'] value against the amount column in your table. You can do this by changing your query to this:
SELECT * FROM scratch_cards WHERE amount='$id' ORDER BY RAND() LIMIT 1
You can also change
while($row = mysqli_fetch_array($query_run))
to
if($row = mysqli_fetch_array($query_run))
although with the change to the query to limit the output to one row this is no longer absolutely necessary.
As has been pointed out, you are vulnerable to SQL injection, you should read this question to see how to resolve that.
Hello I have a database and its data is this please see this image
I need a solution to this problem, the solution is i need the data to be change and not repeating, so when i input a value that is written on the database the displayed value on the textbox will not REPEAT and Change everytime i input the same value.
<form action="" method="POST">
<div class="row col-md-4">
<label>Amount</label>
<input type="text" name="id" class="form-control validate">
<br>
<input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input><br>
//HERE IS WHERE I SUBMIT THE DATA RIGHT NOW IT IS NOT RANDOMIZED WHEN I SUBMIT AGAIN THE SAME VALUE APPEARS
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection, 'qrproject');
if(isset($_POST['search'])){
$id = $_POST['id'];
$query = "SELECT * FROM scratch_cards WHERE amount='$id' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run)) {
?>
<form action="" method="POST">
<input type="text" name="code" value="<?php echo $row['code'] ?>" class="form-control validate" id="mapo">
<input type="text" name="pin" value="<?php echo $row['pin'] ?>" class="form-control validate" id="mact">
<input type="text" name="status" value="<?php echo $row['status'] ?>" class="validate form-control" id="soluong">
<input type="date" name="card_expiration" value="<?php echo $row['card_expiration'] ?>" class="validate form-control" id="cardex">
<input type="number" name="card_validity" value="<?php echo $row['card_validity'] ?>" class="validate form-control" id="cardval">
</form>
<?php
}
}
?>
</form>
Maybe as per your comment, you need unique records, so you can use DISTINCT in your query so by giving DISTINCT to any column, you won't get repeated records.
Here in your case:
SELECT DISTINCT id, amount FROM scratch_cards WHERE amount='$id'
Maybe this can help you to get unique records
i got problem when update idSoal (PK),but for other can do it,the sql code is like this:
==>index.php
<td align="center"><img src="images/edit.png" /></td>
==>editSoal.php
$id_soal=$_GET['idSoal'];
$sql = "SELECT * FROM soal WHERE idSoal = '$id_soal'";
.............
<form id="contactform" action="proses_editSoal.php" method="POST">
<label for="id_soal">ID SOAL</label>
<input id="id_soal" name="id_soal" value="<?php echo $row['idSoal']; ?>" required="" type="text" ">
<label for="soal">SOAL</label>
<input id="nama" name="soal" value="<?php echo $row['soal']; ?>" required="" type="text" >
<input class="buttom" name="submit" id="submit" tabindex="5" value="Edit!" type="submit">
</form>
==>proses_editSoal.php
$id_soal=$_POST['id_soal'];
$soal = $_POST['soal'];
$query="UPDATE soal SET idSoal='$id_soal',soal='$soal' WHERE idSoal='$id_soal'";
for "soal" i can update it. where is my problem?
It seems you are using same variable in SET and WHERE for idSoal value. So you are not changing the value of it.
In mysql you can update any field. Try to print the query and you'll see the problem.
Although variations of this question have been asked before, none of them have helped me solve this puzzle. at present the form I have put together updates the database fine etc, the issue is when I go to edit the field, only the first part of the data is shown for example, if the data was 'Sunny Day' then when i clicked to edit this field all i would get is 'Sunny', so then if i then clicked update, it would edit the database back to Sunny. Am i missing something?!
Thanks in advance.
// Connects to your Database
$query=mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error());
mysql_select_db("cl52-abcdef",$query);
?>
<html>
<body>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$holdesc1=$_POST['holdesc1'];
$holdest1=$_POST['holdest1'];
$rrp1=$_POST['rrp1'];
$cpe1=$_POST['cpe1'];
$ea1=$_POST['ea1'];
$query3=mysql_query("update DealOne set holdesc1='$holdesc1',holdest1='$holdest1',rrp1='$rrp1',cpe1='$cpe1',ea1='$ea1' where id='$id'");
if($query3)
{
header('location:list.php');
}
}
$query1=mysql_query("select holdesc1, holdest1, rrp1, cpe1, ea1 from DealOne where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
1 <input type="text" value=<? echo $query2['holdesc1']; ?> name="holdesc1"><br>
2 <input type="text" value=<? echo $query2['holdest1']; ?> name="holdest1"><br>
3 <input type="text" value=<? echo $query2['rrp1']; ?> name="rrp1"><br>
4 <input type="text" value=<? echo $query2['cpe1']; ?> name="cpe1"><br>
5 <input type="text" value=<? echo $query2['ea1']; ?> name="ea1"><br><br>
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>
Only the first part of the data is showing up because your value attribute values do not have quotes around it. Otherwise, your HTML looks like: <input type="text" value=Sunny day name="holdesc1">
Change your text inputs to the following:
<input type="text" value="<? echo $query2['holdesc1']; ?>" name="holdesc1"><br>
<input type="text" value="<? echo $query2['holdest1']; ?>" name="holdest1"><br>
<input type="text" value="<? echo $query2['rrp1']; ?>" name="rrp1"><br>
<input type="text" value="<? echo $query2['cpe1']; ?>" name="cpe1"><br>
<input type="text" value="<? echo $query2['ea1']; ?>" name="ea1"><br><br>
I am teaching myself code, and after going over PHP & MySQL tutorials, I'm still a little unsure.
I want to create a page in which the user ticks relevant checkboxes, saves the data, and can log back in another time and the ticks are saved.
I've learned how to use data that is in MySQL, but how is data auto-submitted by the user? That's got me stumped...
You need to use a form:
http://www.tizag.com/htmlT/forms.php
<form method="post" action="/your/php/script.php">
Name: <input type="text" size="10" maxlength="40" name="name"> <br />
Password: <input type="password" size="10" maxlength="10" name="password">
<input type="submit" value="Save"/>
</form>
This isn't really a question than can be answered simply, but here's the Simple answer.
First, put your checkboxes in a form:
<form action="page2.php" method="post">
<input type="checkbox" name="cb1" value="SomeValue">SomeValue</input>
</form>
Then, in page2.php, put the data (which is in the $POST array) into the MySql database using the mysql* functions (mysql_connect, mysql_select_db, mysql_query, etc).
Here is an example.
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<form action="" method=post>
<b>Company Name</b> <input name="CompanyName" type="text" value="<?php echo $row['company'] ?>" /><br>
<b>First Name</b> <input name="firstname" type="text" value="<?php echo $row['firstname'] ?>"/><br>
<b>Last Name</b> <input name="lastname" type="text" value="<?php echo $row['lastname'] ?>" /><br>
<input type="submit" value="Submit">
</form>