Second submit button not working like first one - php

Right now I am really confused and have been working on this problem for a couple hours now so I thought I would post on here and maybe get some feedback. So I have a program that runs this search tool on a file. The file either comes from user input generated into a file or a user actually uploading the file. I have 2 submit buttons where the first one goes with user generated files and the second one has to do with a user uploading a file.
The problem I have is that I have the same exact code for the second submit button (All that changes is the way the program gets the file) but for some reason by second submit button doesnt work. Nothing is generated and the execution of the search tool does not occur. I know this because there is not an output file created.
I was wondering if someone knows why my second button doesnt perform like the first button....
Here is my code...
<form method="POST", action="/~cs4380sp15grp4/home/blast.php" class="form-inline">
<textarea id="BlastSearch" type="textarea" name="BlastSearch" class="form-control">
Database:
<select id="database" name="database" class="form-control">
<option value="Archaea">Archaea</option>
</select>
Threshold:
<select id="evalue" name="evalue" class="form-control">
<option value="0.0001">0.0001</option>
<option value="0.001">0.001</option>
</select>
Hits:
<select id="hits" name="hits" class="form-control">
<option value="50">50</option>
<option value="100">100</option>
</select>
<button id="run" type="submit" name="submit" class="btn btn-primary"><span class="glyphicon glyphicon-wrench"></span> Run BLAST</button>
<button type="reset" value="Clear" class="btn btn-info">Clear</button>
</form>
<form enctype="multipart/form-data" action="upload.php" method="POST" class="form-inline"/>
<input type="file" name="fileToUpload" id="fileToUpload" class="form-control"/>
<input type="submit" value="upload" name="upload" class="form-control"/>
<input type="reset" value="reset" name="reset" class="form-control"/>
</form>
<form method="POST", action="/~cs4380sp15grp4/home/blast.php" class="form-inline">
<input type="submit" value="submit file" name="submit2" class="form-control"/>
</form>
<?php
//connects to db
//Insert the values into the database
if(isset($_POST['submit'])){
//declare variables to what the user defines them as
$db = $_POST['database'];
$evalue = $_POST['evalue'];
$sequence = $_POST['BlastSearch'];
$hits = $_POST['hits'];
//create a new .fasta file and put the sequence the user wants to search for in that file
$file = 'uploads/'.$mysqli->insert_id.'.fasta';
$header = ">gi|129295|sp|P01013|OVAX_CHICK GENE X PROTEIN (OVALBUMIN-RELATED)\n";
$current = $header . $_POST['BlastSearch'];
file_put_contents($file, $current);
$userid = $_SESSION['uid'];
//insert the values into the database
$mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', '" . $sequence . "', 'running' , NOW(), NOW())");
$mysqli->query("INSERT INTO `BLAST`(`db_name`, `evalue`, `job_id`) VALUES ('" . $db . "','" . $evalue . "', '".$mysqli->insert_id."')") or die(mysqli_error($mysqli));
//execute the BLAST Tool
// Do this execute statement if the user inputs his own sequence. (Use new.fasta)
exec('/students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/public_html/home/uploads/'.$mysqli->insert_id.'.fasta -m'.$evalue.' -o outputSEQ -v'.$hits.' -b'.$hits);
}
if(isset($_POST['submit2'])){
//declare variables to what the user defines them as
$db = $_POST['database'];
$evalue = $_POST['evalue'];
$sequence = $_POST['BlastSearch'];
$hits = $_POST['hits'];
$userid = $_SESSION['uid'];
//insert the values into the database
$mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', '" . $sequence . "', 'running' , NOW(), NOW())");
$mysqli->query("INSERT INTO `BLAST`(`db_name`, `evalue`, `job_id`) VALUES ('" . $db . "','" . $evalue . "', '".$mysqli->insert_id."')") or die(mysqli_error($mysqli));
exec('/students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/public_html/home/uploads/sample.fasta -m '.$evalue.' -o outputFILE -v'.$hits.' -b'.$hits);
}
$mysqli->close();
?>
I dont understand it. basically the exec function works in the first submit button, but the exec function doesnt work in the second one... I can go into terminal and run the second exec function and it runs perfectly fine. Is it because of the action for the second submit being upload.php? I could see it being that....
EDIT1: I just tried creating a new form specifically for that button and making the action the same as the first submit buttons form. It didnt work though :( any help is appreciated!

Hi there is few issues in your code you pasted.
In first form textarea does not have closing tag.
In third form you have a comma (,) after method="Post"
Your question is about second submit button and you have created 3 forms.

Should that comma after the method=POST be there? I can't actually see why it would make any difference, but it's not going to help. Having multiple forms on a page is valid of course, but it's sometimes better to have just one form and use Javascript behind multiple 'normal' buttons to set the enctype, method, action set and required defaults and then submit the form programatically.

Provide the following hidden inputs in the corresponding forms in order to get the $_POST['submit'] and $_POST['submit2'] in the server side php:
<input type="hidden" value="1" name="submit" />
<input type="hidden" value="1" name="submit2" />
Try this:
<form method="POST", action="/~cs4380sp15grp4/home/blast.php" class="form-inline">
<textarea id="BlastSearch" type="textarea" name="BlastSearch" class="form-control">
Database:
<select id="database" name="database" class="form-control">
<option value="Archaea">Archaea</option>
</select>
Threshold:
<select id="evalue" name="evalue" class="form-control">
<option value="0.0001">0.0001</option>
<option value="0.001">0.001</option>
</select>
Hits:
<select id="hits" name="hits" class="form-control">
<option value="50">50</option>
<option value="100">100</option>
</select>
<input type="hidden" value="1" name="submit" />
<button id="run" type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-wrench"></span> Run BLAST</button>
<button type="reset" value="Clear" class="btn btn-info">Clear</button>
</form>
<form enctype="multipart/form-data" action="upload.php" method="POST" class="form-inline"/>
<input type="file" name="fileToUpload" id="fileToUpload" class="form-control"/>
<input type="submit" value="upload" name="upload" class="form-control"/>
<input type="reset" value="reset" name="reset" class="form-control"/>
</form>
<form method="POST", action="/~cs4380sp15grp4/home/blast.php" class="form-inline">
<input type="hidden" value="1" name="submit2" />
<input type="submit" value="submit file" class="form-control"/>
</form>
<?php
//connects to db
//Insert the values into the database
if(isset($_POST['submit'])){
//declare variables to what the user defines them as
$db = $_POST['database'];
$evalue = $_POST['evalue'];
$sequence = $_POST['BlastSearch'];
$hits = $_POST['hits'];
//create a new .fasta file and put the sequence the user wants to search for in that file
$file = 'uploads/'.$mysqli->insert_id.'.fasta';
$header = ">gi|129295|sp|P01013|OVAX_CHICK GENE X PROTEIN (OVALBUMIN-RELATED)\n";
$current = $header . $_POST['BlastSearch'];
file_put_contents($file, $current);
$userid = $_SESSION['uid'];
//insert the values into the database
$mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', '" . $sequence . "', 'running' , NOW(), NOW())");
$mysqli->query("INSERT INTO `BLAST`(`db_name`, `evalue`, `job_id`) VALUES ('" . $db . "','" . $evalue . "', '".$mysqli->insert_id."')") or die(mysqli_error($mysqli));
//execute the BLAST Tool
// Do this execute statement if the user inputs his own sequence. (Use new.fasta)
exec('/students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/public_html/home/uploads/'.$mysqli->insert_id.'.fasta -m'.$evalue.' -o outputSEQ -v'.$hits.' -b'.$hits);
}
if(isset($_POST['submit2'])){
//declare variables to what the user defines them as
$db = $_POST['database'];
$evalue = $_POST['evalue'];
$sequence = $_POST['BlastSearch'];
$hits = $_POST['hits'];
$userid = $_SESSION['uid'];
//insert the values into the database
$mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', '" . $sequence . "', 'running' , NOW(), NOW())");
$mysqli->query("INSERT INTO `BLAST`(`db_name`, `evalue`, `job_id`) VALUES ('" . $db . "','" . $evalue . "', '".$mysqli->insert_id."')") or die(mysqli_error($mysqli));
exec('/students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/public_html/home/uploads/sample.fasta -m '.$evalue.' -o outputFILE -v'.$hits.' -b'.$hits);
}
$mysqli->close();
?>

Related

Increment id on click in MySQL

I have a problem to visualize the solution for the problem that I have now.
The user is allowed to insert a row in a table.
And I try to display a button (input) +1 who allow the user to increment a column (vote) in a selected row among all created.
The problem is that I don't get the thing for rely incrementation to the desired id.
Here my code :
<form action="" method="post">
<input type="text" name="disease">name
<input name="mainsubmit" type="submit" value="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['mainsubmit']))
{
$nameDisease = $_POST['disease'];
$req = $db->prepare('INSERT into disease(name) VALUES(:name)');
$req->execute(array('name' => $nameDisease));
}
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch())
{
$id = $result['id'];
echo $id ?>
<form action="" method="post"> <input name="secondsubmit" type="submit" value="+1"> </form><?php
if(isset($_POST['secondsubmit']))
{
$db->exec("UPDATE disease SET vote = vote + 1 WHERE id = " .$id);
}
}
Logically, the code above doesn't work but I don't understand how find the solution.
In brief, i want to allow the user to increment a column in a selected row.
Thanks
Edit: Shadow, it's not my problem because your solution is used for automatically chose between INSERT or UPDATE if the line doesn't exist or exist. Me, I want allow the user to create rows and allow he to vote +1 on each of one that exist, and it will not be possible for he to insert a row from the input +1.
I created code snippet similar to your code style.
You have two submit buttons so you need to separate handling of those two requests.
The $id of the item you want to update in the second submit need's to come from hidden value in form.
In order for this to work you need to create table in mysql:
create table disease (id MEDIUMINT NOT NULL AUTO_INCREMENT, name VARCHAR(20), vote INTEGER, PRIMARY KEY (id)); - for example like this
<html>
<body>
<form action="" method="post">
<input type="text" name="disease">name
<input name="mainsubmit" type="submit" value="submit">
</form>
</body>
</html>
<?php
$db = new PDO('mysql:dbname=phpapp;host=db', 'root', 'phpapptest');
if (isset($_POST['mainsubmit'])) {
$nameDisease = $_POST['disease'];
$req = $db->prepare('INSERT into disease (name, vote) VALUES(:name, 0)');
$req->bindParam(':name', $nameDisease);
$req->execute();
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch()) { ?>
<form action="" method="post">
<p><?php echo $result['name'] . " : " . $result['vote'];?>
<input name="secondsubmit" type="submit" value="+1" />
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
</p>
</form>
<?php }
}
if (isset($_POST['secondsubmit'])) {
$req = $db->prepare("UPDATE disease SET vote = vote + 1 WHERE id = " . $_POST['id']);
$req->execute();
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch()) {?>
<form action="" method="post">
<p><?php echo $result['name'] . " : " . $result['vote'];?>
<input name="secondsubmit" type="submit" value="+1" />
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
</p>
</form>
<?php }
}
?>

null column in protection from sql injection

hi i am having a contact form in my website where user can optionaly fill some of the fields and after click on submit button data save in to the database all of this worked fine until i decide to sanitize my code from sql injection as i mentioned at first before trying to sanitize it from sql injection it worked properly as i showed in below code
<form method="Post" action="">
<input type="text" name="name" />name
<select dir="rtl" style="width: 173px;" name="case" >
<option value="" disabled selected hidden>اplease choose</option>
<option value='rent'>rent</option>
<option value='sell'>sell</option>
</select >
<input type="checkbox" name="check1" value='a'>apartment<br>
<input type="submit" value="submit" />
</form>
<?php
include("config.php");
if(isset($_POST['submit'])){
$date_clicked = date('Y-m-d H:i:s');
}
//insert to database
$insert =mysqli_query($connect,"INSERT INTO $db_table VALUES (to simplify code i do not write this part)");
}
?>
now i have to fill all the dropdown lists and checkboxes otherwise it gives error "column '' can not be null". also i can not insert date and time into database it gives the same error. here is my code when i protect it fron sql injection:
<form method="Post" action="">
<input type="text" name="name" />name
<select dir="rtl" style="width: 173px;" name="case" >
<option value="" disabled selected hidden>اplease choose</option>
<option value='rent'>rent</option>
<option value='sell'>sell</option>
</select >
<input type="checkbox" name="check1" value='a'>apartment<br>
<input type="submit" value="submit" />
</form>
<?php
include("config.php");
if(isset($_POST['submit'])){
$date_clicked = date('Y-m-d H:i:s');
}
if(isset($_POST['submit'])){
//insert to database
$query = mysqli_prepare($connect, "INSERT INTO $db_table VALUES (?,?,?,?)");
/* bind parameters for markers */
mysqli_stmt_bind_param( $query, "ssss", $_POST[name],$_POST['check1'],$_POST['case'],$_POST['date_clicked']);
// execute query
if ( mysqli_stmt_execute($query) ) {
echo "Successfully inserted " . mysqli_affected_rows($connect) . " row";
} else {
echo "Error occurred: " . mysqli_error($connect);
}
}
?>
please help me
Make sure that your variables exist. This is necessary because your checkbox, for example, will be null if not checked and that could be a problem for the table you are using. You could set defaults and then insert it.
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$check1 = !empty($_POST['check1']) ? $_POST['check1'] : '';
$case = !empty($_POST['case']) ? $_POST['case'] : '';
$date_clicked = date('Y-m-d H:i:s');
// prepare and bind
$stmt = $connect->prepare("INSERT INTO `$db_table` (`name`, `check1`, `case`, `date_clicked`) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $name, $check1, $case, $date_clicked);
$stmt->execute();
$stmt->close();

Inserting multiple row in database using php form

I created a form which can add more input field for product_name and quantity of the product with the help of jquery, this is the demo where you can add more input field in the form.
the problem is when i submit the form only the last product will submit into my database the rest of the product will not submitted.
this is my query
<?php
if(isset($_POST['submit'])){
//process the form
$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$product_description = $_POST["product_description"];
$quantity = $_POST["quantity"];
$status = $_POST["status"];
$query = "
INSERT INTO orders (
date, customer_name, product_description, quantity, status
) VALUES (
'$date', '$customer_name', '$product_description',$quantity,$status
)";
$order_set = mysqli_query($connection, $query);
if($order_set){
redirect_to("index.php");
}
} else {
// failed
}
?>
My Form
<form action="order.php" method="post">
<div class="newOrder">
<p><span>Date</span><input type="date" value="2014-12-01" name="date" /></p>
<p><span>Name</span>
<select name="customer_name">
<?php
while($customer = mysqli_fetch_assoc($customers_set)){ ?>
<option><?php echo $customer['customer_name']; ?></option>
<?php } ?>
<?php mysqli_free_result($customers_set); ?>
</select>
</p>
<div id="input_fields">
<p><span>Product Description</span>
<select name="product_description">
<?php
while($product = mysqli_fetch_assoc($product_set)){ ?>
<option><?php echo $product['product_description']; ?></option>
<?php } ?>
<?php mysqli_free_result($product_set); ?>
</select>
<input value="0" type="text" name="quantity" />
</p>
</div>
Add More Product
<p class="radio">
<input type="radio" name="status" value="0" checked />For delivery
<input type="radio" name="status" value="1" />For payment confirmation
<input type="radio" name="status" value="2" />Reserved items
</p>
<input type="submit" name="submit" value="Create Order" />
</div>
</form>
any body have any idea how to submit all product and quantity input in input field will be save in database.
Wrap your values inside your database connection. Consider this from one of my old course. Notice is a different code however working perfectly.
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];
$dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool', 'aliendatabase')
or die('Error connecting to MySQL server.');
$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
mysqli_close($dbc);
The input fields have the same name? So I guess thats why only the last one get inserted.
You have to loop the INSERT query foreach product you add, this includes quantity.
You should allso sanitize the input value before you inserting it to a query.
When you inserting multiple queries, you shouldn't do that from the php loop. Is not efficient because you are executing multiple queries instead one or two. You can loop trough the results sent from the form, clean it and prepare it for database insertion, and after that build a query based on that results. Look at here for the inserting multiple rows at once into a database :
(Insert multiple rows with one query MySQL)
you need to use foreach
foreach ($_POST['quantity'] as $quantity) {
//insert code
}

Data insertion issues with HTML form and PHP to mySQL

I have seen few posts in SO related to the same scenario as mine but did not find a proper resolution. So am posting question with my problem stuff.
I have an HTML form
<form method="post" id="myForm">
<label for="e_name">Name</label>
<input name="e_name" id="emp_name" value="" type="text" data-theme="a">
<label for="date">Date</label>
<input name="date" id="emp_dob" value="" data-theme="a">
<label for="gender">Gender</label>
<select name="gender" id="emp_gender" data-role="slider" data-theme="a" data-inline="true">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label for="address">Address</label>
<textarea name="address" id="emp_address" value="" type="text" data-theme="a"></textarea><br><br>
<input type="button" id="insert" value="Submit">
</form>
<div id="someElement"></div>
And I have the following to perform my form elements submission to a PHP page-
$(document).ready(function(){
$("#insert").click(function(e) {
e.preventDefault();
alert("am in the Insert function now");
$.ajax({
cache: false,
type: 'POST',
url: 'insert.php',
data: $("#myForm").serialize(),
success: function(d) {
$("#someElement").html(d);
}
});
});
});
Here is my PHP -
<?php
$con=mysqli_connect("localhost","root","root","employee");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name =" ";
$dob =" ";
$gender =" ";
$address =" ";
if(isset($_POST['emp_name'])){ $name = $_POST['emp_name']; }
if(isset($_POST['emp_dob'])){ $dob = $_POST['emp_dob']; }
if(isset($_POST['emp_gender'])){ $gender = $_POST['emp_gender']; }
if(isset($_POST['emp_address'])){ $address = $_POST['emp_address']; }
echo $name;
echo $dob;
echo $gender;
echo $address;
$sql="INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('$name', '$gender', '$address')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Now what happens is, when I enter some values in my form and click on Submit, action performs well and a new row inserts in the database. But all the rows are empty. They're just empty, not even "NULL".
I tried to echo my field values in the PHP but there is no output there. My "1 Record Added" has come-up well, but no form values appear here.
Kindly help me sorting this out.
Thanks in advance.
$_POST[] references to the name attribute of html-tag, not id.
For example, $_POST['emp_name'] should be $_POST['e_name']
Furthermore, don't encapsulate your variables with single quotes:
"INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('$name', '$gender', '$address')";
Do this instead:
"INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('" . $name . "', '" . $gender . "', '" . $address . "')";
Or use bind_param() from mysqli ofcourse!
Make the id and name of your input elements same
<form method="post" id="myForm">
<label for="e_name">Name</label>
<input name="emp_name" id="emp_name" value="" type="text" data-theme="a">
<label for="date">Date</label>
<input name="emp_dob" id="emp_dob" value="" data-theme="a">
<label for="gender">Gender</label>
<select name="emp_gender" id="emp_gender" data-role="slider" data-theme="a" data-inline="true">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label for="address">Address</label>
<textarea name="emp_address" id="emp_address" value="" type="text" data-theme="a"></textarea><br><br>
<input type="button" id="insert" value="Submit">
</form>
Otherwise change your $_POST array keys.Because you will get the keys of $_POST array will be the name of input elements.But i recommend you to mak ethe name and id same

PHP Form that updates multiple form element to different columns

I am trying to build a simple form that will update my mySql database.
I can succeed if I only have 1 form element (input) on the page but I can not figure out how to have more than one form element (inputs) per pgae.
When I add more than one input the database will not add any of the content.
I know that my code is close, but I am at a loss as the exactly where and what to do about it.
P.S. - I am still new to this and am learning ...
Here is what I have
<?php
$host = 'hostName';
$user = 'userName';
$password = 'password';
$link = mysql_connect($host, $user, $password);
$selected = mysql_select_db('dbName', $link);
if(!isset($_POST['text-input']))
{
echo '<html>
<body>
<form action="post.php" method="post">
<input type="text" name="text-input" id="text-input" value="Update MyDataColumn" style="width:300px;" />
<input type="submit" value="Submit" />
</form>
</body>
</html>'; }
else {
$form_input = $_POST['text-input'] ;
mysql_query('INSERT INTO `tableName` (columnName) VALUES ("' . $form_input . '");');
echo '
<html>
<body>
<script type="text/javascript">
alert(\'Database now contains: <?php echo $form_input ?>. Redirecting...\');
window.location = \'http://url.com\';
</script>
</body>
</html>';
}
?>
I would like to sort out how to post to numerous columns within the same db/table.
Ok, from the answers below I have modified the code to look like this:
<?php
$host = 'dbHost';
$user = 'dbUser';
$password = 'dbPassword';
$link = mysql_connect($host, $user, $password);
$selected = mysql_select_db('dbName', $link);
if(!isset($_POST['text-input']))
{
echo '
<form action="index.php" method="post">
<input type="text" name="text-input" id="text-input" value="Update itemName" style="width:300px;" />
<input type="text" name="text-input2" id="text-input2" value="Update itemDescription" style="width:300px;" />
<input type="text" name="text-input3" id="text-input3" value="Update productID" style="width:300px;" />
<input type="text" name="text-input4" id="text-input4" value="Update itemPrice" style="width:300px;" />
<input type="submit" value="Submit" />
</form>'
; }
else {
$form_input = $_POST['text-input'] ;
$form_input2 = $_POST['text-input2'] ;
$form_input3 = $_POST['text-input3'] ;
$form_input4 = $_POST['text-input4'] ;
mysql_query('INSERT INTO `items` (itemName, itemDescription, productID, itemPrice)
VALUES ("' . $form_input . '", "' . $form_input2 . '", "' . $form_input3 . '", "' . $form_input4 . '");
echo '
<html>
<body>
<script type="text/javascript">
alert(\'Database has been updated. Redirecting to previous url.\');
window.location = \'http://url.com\';
</script>
</body>
</html>';
}
?>
What happens with this code is I get a
syntax error, unexpected '>'
You just have to modify your query to set multiple columns.
HTML:
<form action="post.php" method="post">
<input type="text" name="text-input" id="text-input" value="Update MyDataColumn" style="width:300px;" />
<input type="text" name="text-input2" id="text-input2" value="Update MyDataColumn2" style="width:300px;" />
<input type="text" name="text-input3" id="text-input3" value="Update MyDataColumn3" style="width:300px;" />
<input type="submit" value="Submit" />
</form>
PHP:
$form_input = $_POST['text-input'] ;
$form_input2 = $_POST['text-input2'] ;
$form_input3 = $_POST['text-input3'] ;
mysql_query('INSERT INTO `tableName` (columnName, columnName2, columnName3) VALUES ("' . $form_input . '","' . $form_input2 . '","' . $form_input3 . '");');
In your HTML output you need to add another field like this one (already in your code):
<input type="text" name="text-input" id="text-input"
value="Update MyDataColumn" style="width:300px;" />
BUT, it must have a different "name" (!);
<input type="text" name="text-input2" id="text-input2"
value="Another Input" style="width:300px;" />
Now we fetch the new variable ("text-input2") out of the $_POST array like this:
$form_input = $_POST['text-input']; // Your code...
$form_input2 = $_POST['text-input2']; // for the new input field
And we need to change the SQL command to this:
mysql_query('INSERT INTO `tableName` (columnName) VALUES ("' . $form_input . '");
mysql_query('INSERT INTO `tableName` (columnName) VALUES ("' . $form_input2 . '");
The above SQL command would add both form inputs as a seperate row in your table, they are not connected to eachother.
To have them in the same row you need to change your table first and add another column to it. The SQL command for that is:
ALTER TABLE tableName ADD columnName2 VARCHAR(255);
This will add another column named 'columName2' to the table. And we finally can add both values as one row of the table:
mysql_query('INSERT INTO `tableName` (columnName, columName2)
VALUES ("' . $form_input . '", "' . $form_input2 . '");
That's it ;-)

Categories