Will only display first part of mysql_fetch_array - php

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>

Related

PHP, MySQL, Crud - "Fetching" mysql data into my update page, displaying data into form field

Hopefully this makes sense. Been working on a project for awhile and i'm just about done! Just stuck and not sure why this isn't working.
I have an entire Crud app used with php and mysql. Everything works correctly except for when I go to my update.php page. When I click update on the home page, it takes me to the update page where I can fill out fields to update the mysql data. That all works fine! What I am having issues with is when I go to the update page, the form fields are blank. I want them to be pre-filled with the data that's already in there, so "fetching" the data. That way, the user doesn't have to go back and look at the data and then re-type all the information again instead of just backspacing and updating just the field data that they want. Does that make sense? Here's my code. Notice and focus where I have the $fetch variable...I know it's a lot. I tried to remove all the really unecessary stuff. If you are able to look at this and see an easy fix, please let me know! Struggling here. Below is my "update.php" page:
<?php
session_start();
$login_session=$_SESSION['login_user'];?>
<?php
if(!$_SESSION['login_user']){
header("location: login.php");
die;
}
?>
<p>Go Back |
Logout </p>
<?php
include 'scripts/db.php';
if(isset($_GET['id']) && is_numeric($_GET['id']))
{
$id=$_GET['id'];
}
$select=mysql_query("SELECT * FROM Order_1 WHERE id='$id'");
$fetch=mysql_fetch_array($select);
if(isset($_POST['update']))
{
$name=$_POST['name'];
$product=$_POST['product'];
$size=$_POST['size'];
$price=$_POST['price'];
$shipping=$_POST['shipping'];
$shipped=$_POST['shipped'];
$delivered=$_POST['delivered'];
$update=mysql_query("UPDATE Order_1 SET Name='$name',Product='$product',Size='$size',Price='$price',Shipping='$shipping',Shipped='$shipped',Delivered='$delivered' WHERE id='$id' ");
if($update) {
?>
<script>alert('Record updated successfully!');</script>
<?php
echo '<script type="text/javascript">window.location="home.php";</script>';
}
else
{
?>
<script>alert('Error updating record.');</script>
<?php
}
}
?>
<div class="container">
<h1>Edit Your Order and Click Submit:</h1>
<div class="updateForm">
<form method="post">
<p>Name: <input type="text" name="name" placeholder="First, Last" value="<?php echo $fetch['name']; ?>" /></p>
<p>Product: <input type="text" name="product" size="15" placeholder="T-Shirt, Shoes, etc..." value="<?php echo $fetch['product']; ?>" /></p>
<p>Size: <input type="text" name="size" size="10" placeholder="S, M, L, XL, XXL" value="<?php echo $fetch['size']; ?>" /></p>
<p>Price: <input type="text" name="price" size="10" placeholder="$0.00" value="<?php echo $fetch['price']; ?>" /></p>
<p>Shipping: <input type="text" name="shipping" size="10" placeholder="$0.00" value="<?php echo $fetch['shipping']; ?>" /></p>
<p>Shipped?
Yes<input type="radio" name = "shipped" value="Yes" value= "<?php echo $fetch['Yes']; ?>"/>
No<input type="radio" name = "shipped" value="No" value = "<?php echo $fetch['No']; ?>" /></p>
<p>Delivered?
Yes<input type="radio" name = "delivered" value="Yes" value="<?php echo $fetch['Yes']; ?>" />
No<input type="radio" name = "delivered" value = "<?php echo $fetch['No']; ?>" /></p>
<p><input type="submit" name="update" value="Submit" /></p>
</form>
</div>
</div>

How to use values in the URL in PHP

I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

Fetch MySQL table data with specific value

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.

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

Categories