Auto submit data to MySQL? - php

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>

Related

populate input field with PHP variable

I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>

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

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.

Javascript Undefined with PHP & MySQL

I have 2 PHP pages...
INDEX.PHP
with this code:
<form method="post" action="" name="f1">
<input type="text" name='p_name' size='50'><br>
<input type="text" name='p_name2' size='50'><br>
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("index2.php","Ratting","width=550,height=170,left=150,top=200,toolbar=1,status=1,");>Click here to open the child window</a>
</form>
and INDEX2.PHP
with this avascript code:
<script langauge="javascript">
function post_value()
{
opener.document.f1.p_name.value = document.frm.c_name.value;
opener.document.f1.p_name2.value = document.frm.c_name2.value;
self.close();
}
</script>
and this PHP/HTML:
<form name="frm" method="post" action="">
<?php
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
echo '<input type="text" name="c_name" size="50" value="'.$result["sequence"].'" /><br>
<input type="text" name="c_name2" size="50" value="'.$result["company"].'" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>';
}
?>
</form>
basically, when you go to index.php, you click the link to open the popup window index2.php and that then lists customers from a database and gives each one 2 text boxes - one for the sequence/id of the customer and the other for the company name and one submit button per row.
When the button is pressed, it runs the javascript function post_value(); which puts the values from the database/child popup window (index2.php) into the text boxes in the parent window (index.php)
when i run this code, it just puts the word undefined in box boxes on the parent page, however if i remove the while loop in php and it just displays the one row from the database of customers it works fine.
its like it doesn't like the while loop in php but i cannot work out why.
any help would be much appreciated.
Edit - I just realized that your while loop is going to generate something that looks like:
<input type="text" name="c_name" size="50" value="A1" /><br>
<input type="text" name="c_name2" size="50" value="B1" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>
<input type="text" name="c_name" size="50" value="A2" /><br>
<input type="text" name="c_name2" size="50" value="B2" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>
<input type="text" name="c_name" size="50" value="A3" /><br>
<input type="text" name="c_name2" size="50" value="B3" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>
This isn't going to work because you have multiple elements named "c_name" and "c_name2" within the same form. I think what you want to do is put your form element inside the while loop like this:
<?php
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$ctr = 0;
while($result=mysql_fetch_array($rs))
{
echo '<form name="frm' . $ctr . '" method="post" action="">
<input type="text" name="c_name" size="50" value="'.$result["sequence"].'" /><br>
<input type="text" name="c_name2" size="50" value="'.$result["company"].'" /><br>
<input type=button value=\'Submit\' onclick=\'post_value(' . $ctr . ');\'><br><br>'
</form>';
$ctr++;
}
?>
Notice how I created a $ctr variable so you can identify each form and how it is passed into post_value. You'll need to use that to grab the correct form like this:
<script langauge="javascript">
function post_value(ctr)
{
opener.document.f1.p_name.value = document.forms["frm" + ctr].c_name.value;
opener.document.f1.p_name2.value = document.forms["frm" + ctr].c_name2.value;
self.close();
}
</script>
By default, mysql_fetch_array returns an array with numeric keys (i.e. 0, 1, 2, etc). If you want to retrieve records by their column name, you need to use:
while ($result = mysql_fetch_array($rs, MYSQL_ASSOC))
Look at example 3 here:
http://php.net/manual/en/function.mysql-fetch-array.php

Inserting Multiple rows in Mysql table with PHP

I am in a fix here. I have code that does not insert multiple data into mysql table with one form. Here's my code-
<?php
if (isset($_POST['submitted'])) {
include('config.php');
foreach($_POST['name'] as $row=>$nam) {
$name=$nam;
$class=$_POST['class'][$row];
echo "<br/>" . $name . "<br/>" . $class;
$sql="INSERT INTO multiple (name, class) VALUES ('$name','$class')";
}
if (!mysql_query($sql)) die('Error: ' . mysql_error());
echo "1 record added";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Name1:
<input id="name" name="name[]" type="text">
<input type="hidden" name="submitted" value="true" />
</label>
<label> Class1:
<input id="class" name="class[]" type="text">
</label>
<label>Name2:
<input id="name" name="name[]" type="text">
</label>
<label>Class2:
<input id="class" name="class[]" type="text">
</label>
<label>Name3:
<input id="name" name="name[]" type="text">
</label>
<label>Class3:
<input id="class" name="class[]" type="text">
</label>
<label>Name4:
<input id="name" name="name[]" type="text">
</label>
<label>Class4:
<input id="class" name="class[]" type="text">
</label>
<label>Name5:
<input id="name" name="name[]" type="text">
</label>
<label>Class5:
<input id="class" name="class[]" type="text">
</label>
<input value="Add" type="submit">
</form>
When I press the submit button nothing inserts in the mysql table. Only empty fields are created. If I insert 5 text field I get 5 empty fields in sql table.
Imroz, your use of [] as part of the names of your input elements (not id's) example name="class[]" when the form is posted it builds an array. The post object PHP would recognize would be $_POST['class']
But that being an array means you have to handle it slightly different before inserting it into your database as you can't just (well maybe you can) drop an array into the DB
if you did
for($x=0;$x < count($_POST['class']); $x++)
{
echo $_POST['class'][$x].'<br>';
}
you would be able to see all your posted inputs from the inputs with the name class[]
of course this is a core example of what you need to do overall, but I am just trying to express whats going on with your posted data.
i think your inserting query is problem.....try like this
$query = mysql_query("INSERT INTO category VALUES('','$name','$class','')") or die(mysql_error());
You have the } in the wrong place.
The } right after the $sql= should be moved after the echo "1 record added";
I reformatting your code to use proper indenting and the error was obvious.

Categories