How to insert multiple inputs with same input name - php

Assume that i have table: transactions with 7 columns:id, date, amount, detail, type, purpose, location. I want to be able to input 3 rows once i click on submit button. In my confiq.php there is connection set up in variable $query.
This is the code that I have in my insertmultiple.php (later it will be included in my overview.php in a table as 3 rows).
Problem: I want to insert 3 lines of data from html form into new rows in table. Do yousee where I make mistake? I have a feeling its somewhere where I start condition if.
Thanks to everyone.
<?php
include('config.php');
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
foreach( $date as $key => $d ) {
$sql = "INSERT INTO transactions (date, detail, type)
VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
if ( $sql === TRUE) {
mysqli_query ($query,$sql);
} else {
echo "Error: " . $sql . "<br>" . $query->error;
}
}
?>
<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>

After preparing the mysqli_connect in ($CONNECTION):
$CONNECTION = mysqli_connect("IP_ADDRESS", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DBNAME");
then:
foreach( $date as $key => $d ) {
$sql = "INSERT INTO transactions (date, detail, type)
VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
if ( !(mysqli_query($CONNECTION, $sql) === TRUE) ) {
echo "Error: " . mysqli_error($CONNECTION) . "<br>" . $sql->error;
}
}

Your test is wrong, $sql will never be true, it is a string. Try this :
<?php
include('config.php');
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
foreach( $date as $key => $d ) {
$sql = "INSERT INTO transactions (date, detail, type) VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
if (mysqli_query ($query,$sql) === FALSE) {
echo "Error: " . $sql . "<br>" . $query->error;
}
}
?>
<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>
EDIT : I'm assuming config.php defines $query var and open the connection. That being said, you should really use PDO and prepared statements to avoid SQL injection.

try this code:
note: read the comments
<?php
include('config.php');
//added an if statment to check if the user have pressed submit (<input type="submit" **name="submit"** id="submit_button" style="width: 50px">)
// this will insure that the query wont start unless data has been sent
if (isset($_POST['submit'])) {
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
foreach ($date as $key => $d) {
$sql = "INSERT INTO transactions (date, detail, type) VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";
// if ($sql === TRUE) { //the $sql will not be true it will be the equal to to the string of the query
// mysqli_query($query, $sql);
// } else {
// echo "Error: " . $sql . "<br>" . $query->error;
// }
if (!mysqli_query($query, $sql)) { //tries to perform the query, if it doesnt work prints the error
echo "Error: " . $sql . "<br>" . $query->error;
}
}
}
?>
<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>

Hi there, good example. I had the same problem and there is a difference between grant all privileges on *.* to root#localhost identified by 'password' and grant all privileges on *.* to root#'%' identified by 'password'
if you are using root on localhost you might as well just wanna run the grant all privileges on *.* to root#localhost identified by 'password'
For Your HTML and PHP scrip have
<?php
include 'db_config.php';
if (isset($_POST['date']) && isset($_POST['detail']) && isset($_POST['type'])) {
$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];
for ($i = 0; $i < count($date); $i++) {
$sql = "INSERT INTO transcations (date, detail, type) " .
" VALUES ('$date[$i]', '$detail[$i]', '$type[$i]')";
//$conn is the name of your connection string
$results = mysqli_query($conn, $sql);
if (!$results) {
echo "Error: " . $sql . "<br>" . $query->error;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post" action="">
<table>
<tr>
<td>
<input type="text" name="date[]" />
</td>
<td>
<input type="text" name="detail[]" />
</td>
<td>
<input type="text" name="type[]" />
</td>
</tr>
<tr><td>
<input type="text" name="date[]" />
</td>
<td>
<input type="text" name="detail[]" />
</td>
<td>
<input type="text" name="type[]" />
</td>
</tr>
<tr>
<td>
<input type="text" name="date[]" />
</td>
<td>
<input type="text" name="detail[]" />
</td>
<td>
<input type="text" name="type[]" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</td>
</tr>
</table>
</form>
</body>
</html>

Related

inserting multiple checkbox values and inputs into a single sql databse with php

Good day. I am having this form to enter multiple
checkboxes and inputs into one row into a sql database. I will get
the one to enter but not the other or the other way around. Will you
please assist me, i am new to php and sql. Thanks.
========================================================================
<form method="post" align="center" action="insertsupplier.php">
<table align="center">
<td>
<label>Supplier ID:</label></br>
<input type="text" id="supplier_id" name="supplier_id" /></br></br>
<label>Company Name:</label></br>
<input type="text" id="company_name" name="company_name" /></br></br>
</td>
<td>
<label>BEE Level:</label></br>
<input type="text" id="bee" name="bee" /></br></br>
<label>Telephone Number:</label></br>
<input type="text" id="tel" name="tel" /></br></br>
</td>
<td>
<label>Contact Person:</label></br>
<input type="text" id="contact" name="contact" /></br></br>
<label>Cell Number:</label></br>
<input type="text" id="cell" name="cell" /></br></br>
</td>
<td>
<label>Email Address:</label></br>
<input type="text" id="email" name="email" /></br></br>
<label>Based:</label></br>
<input type="text" id="based" name="based" /></br></br>
</td>
</table>
<label>Address:</label></br>
<textarea rows="5" cols="50" id="address" name="address"></textarea></br></br>
<table align="center">
<tr>
<label>Trade in:</label></br>
<td>
<input type="checkbox" name="trade[]" value="Air_conditioners">Air Cons</option></br>
<input type="checkbox" name="trade[]" value="Building_Maintanance">Building Maintanance</option></br>
<input type="checkbox" name="trade[]" value="BoreHole">BoreHole</option></br>
<input type="checkbox" name="trade[]" value="Booms">Booms</option></br></br>
</td>
<td>
<input type="checkbox" name="trade[]" value="Cameras">Cameras</option></br>
<input type="checkbox" name="trade[]" value="IT">IT</option></br>
<input type="checkbox" name="trade[]" value="VIF Equipment">VIF Equipment</option></br>
<input type="checkbox" name="trade[]" value="UPS">UPS</option></br></br>
</td>
<td>
<input type="checkbox" name="trade[]" value="WIMS">WIMS</option></br>
<input type="checkbox" name="trade[]" value="Loops">Loops</option></br>
<input type="checkbox" name="trade[]" value="Scale">Scale</option></br>
<input type="checkbox" name="trade[]" value="Generator">Generator</option></br></br>
</td>
<td>
<input type="checkbox" name="trade[]" value="Roads_and_Fittings">Roads and Fittings</option></br>
<input type="checkbox" name="trade[]" value="Fire_Equipment">Fire Equipment</option></br>
<input type="checkbox" name="trade[]" value="Minisub">Minisub</option></br>
<input type="checkbox" name="trade[]" value="Mimic_Panels">Mimic Panels</option></br></br>
</td>
<td>
<input type="checkbox" name="trade[]" value="Furniture">Fruniture</option></br>
<input type="checkbox" name="trade[]" value="Pest_Control">Pest Control</option></br>
<input type="checkbox" name="trade[]" value="Telephone">Telephone</option></br>
<input type="checkbox" name="trade[]" value="Internet">Internet</option></br></br>
</td>
<td>
<input type="checkbox" name="trade[]" value="Chlorinator">Chlorinator</option></br>
<input type="checkbox" name="trade[]" value="Lawn_Mowers">Lawn Mowers</option></br>
<input type="checkbox" name="trade[]" value="Electrical">Electrical</option></br>
<input type="checkbox" name="trade[]" value="Hygiene">Hygiene</option></br></br>
</td>
</tr>
</table>
<td>
<label>Added By:</label></br>
<input value="<?php echo $userRow['userName']; ?>" type="text" id="added_by" name="added_by" /></br></br>
</td>
<tr>
<label>Notes:</label></br>
<textarea rows="5" cols="100" id="notes" name="notes"></textarea></br></br>
<input type="submit" value="Submit">
</form>
$url='localhost';
$username = "root";
$password = "";
$dbname = "helpdesk";
$checkbox1 = $_POST['trade'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk.= $chk1.",";
}
$conn = mysqli_connect($url, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO suppliers (trade)VALUES( '$chk' );";
$sql="INSERT INTO suppliers (company_name, supplier_id, telephone_no, contact_person, cell, email, based, address, bee_level, notes, added_by)
VALUES
('$_POST[company_name]','$_POST[supplier_id]','$_POST[tel]','$_POST[contact]','$_POST[cell]','$_POST[email]','$_POST[based]','$_POST[address]','$_POST[bee]','$_POST[notes]','$_POST[added_by]')";
ini_set('display_errors',10);
// Aliases for form selection
$company_name = $_POST["company_name"];
$supplier_id = $_POST["supplier_id"];
$tel = $_POST["tel"];
$contact = $_POST["contact"];
$cell = $_POST["cell"];
$email = $_POST["email"];
$based = $_POST["based"];
$address = $_POST["address"];
$bee = $_POST["bee"];
$notes = $_POST["notes"];
$added_by = $_POST["added_by"];
if (mysqli_query($conn, $sql))
{
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
echo "<script>setTimeout(\"location.href = 'addsupplier.php';\",2500);</script>";
unset($_POST);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
};
mysqli_close($conn);
This will work but it is kind of hokey...
<?php
$url='localhost';
$username = "root";
$password = "";
$dbname = "helpdesk";
$checkbox1 = $_POST['trade'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk.= $chk1.",";
}
$conn = mysqli_connect($url, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql1 = "INSERT INTO suppliers (trade)VALUES( '$chk' );";
$sql2 = "INSERT INTO suppliers (company_name, supplier_id, telephone_no, contact_person, cell, email, based, address, bee_level, notes, added_by) VALUES ('$_POST[company_name]','$_POST[supplier_id]','$_POST[tel]','$_POST[contact]','$_POST[cell]','$_POST[email]','$_POST[based]','$_POST[address]','$_POST[bee]','$_POST[notes]','$_POST[added_by]')";
ini_set('display_errors',10);
// Aliases for form selection
$company_name = $_POST["company_name"];
$supplier_id = $_POST["supplier_id"];
$tel = $_POST["tel"];
$contact = $_POST["contact"];
$cell = $_POST["cell"];
$email = $_POST["email"];
$based = $_POST["based"];
$address = $_POST["address"];
$bee = $_POST["bee"];
$notes = $_POST["notes"];
$added_by = $_POST["added_by"];
// Your first query
if (mysqli_query($conn, $sql1))
{
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
echo "<script>setTimeout(\"location.href = 'addsupplier.php';\",2500);</script>";
unset($_POST);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
};
mysqli_close($conn);
// Your second query.
if (mysqli_query($conn, $sql2))
{
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
echo "<script>setTimeout(\"location.href = 'addsupplier.php';\",2500);</script>";
unset($_POST);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
};
mysqli_close($conn);

The code is right but I can't enter a data into the database

I'm a beginner in php. The code seemed right but when I try to insert the data from the form to the database, it doesn't enter to the database. What is wrong with my code?
Here's my code
addemployee.php
echo '<form action="addemployee.php" method="POST">';
echo '<table align ="center">';
echo '<tr><td>First Name: </td> <td><input type="text" name="first" pattern="[A-Za-z]{1,}" title="A-Z only" required></td> </tr>';
echo '<tr><td>Middle Initial: </td><td><input type="text" name="middle"></td></tr>';
echo '<tr><td>Last Name:</td><td> <input type="text" name="last"></td></tr>';
echo '<tr><td>Contact Number:</td><td> <input type="tel" name="contact" maxlength="11"></td></tr>';
echo '<tr> <td>Province: </td><td> <input type="text" name="provincee"></td></tr>';
echo '<tr> <td>City: </td><td> <input type="text" name="cityy"></td></tr>';
echo '<tr> <td>Username:</td><td> <input type="email" name="usernamee"></td></tr>';
echo '<tr><td>Password:</td><td> <input type="password" name="pass"></td></tr>';
echo '</table>';
echo '<br> <input type="Submit" name="submitt">';
if(isset($_POST['submitt'])){
$firstname=$_POST['first'];
$middleinitial=$_POST['middle'];
$lastname=$_POST['last'];
$contactnumber=$_POST['contact'];
$province= $_POST['provincee'];
$city =$_POST['cityy'];
$username=$_POST['usernamee'];
$password=$_POST['pass'];
$type= 'employee';
$query=("INSERT INTO usertbl (fname,middeinitial,lname,contactnum,province,city,username,password,type) VALUES ('$firstname','$middleinitial','$lastname','$contactnumber','$province',
'$city','$username','$password','$type')");
mysqli_query($db, $query);
}
echo '</form>';
Please check the changes:-
addemployee.php
<!-- use html form not php generated form -->
<form action="addemployee.php" method="POST">
<table align ="center">
<tr><td>First Name: </td> <td><input type="text" name="first" pattern="[A-Za-z]{1,}" title="A-Z only" required></td> </tr>
<tr><td>Middle Initial: </td><td><input type="text" name="middle"></td></tr>
<tr><td>Last Name:</td><td> <input type="text" name="last"></td></tr>
<tr><td>Contact Number:</td><td> <input type="tel" name="contact" maxlength="11"></td></tr>
<tr> <td>Province: </td><td> <input type="text" name="provincee"></td></tr>
<tr> <td>City: </td><td> <input type="text" name="cityy"></td></tr>
<tr> <td>Username:</td><td> <input type="email" name="usernamee"></td></tr>
<tr><td>Password:</td><td> <input type="password" name="pass"></td></tr>
</table>
<br>
<input type="Submit" name="submitt">
</form>
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
if(isset($_POST['submitt'])){
$firstname=$_POST['first'];
$middleinitial=$_POST['middle'];
$lastname=$_POST['last'];
$contactnumber=$_POST['contact'];
$province= $_POST['provincee'];
$city =$_POST['cityy'];
$username=$_POST['usernamee'];
$password=$_POST['pass'];
$type= 'employee';
$db = mysqli_connect ('hostname','username','password','dbname') or die(mysqli_connect_error()); // provide your details here
$query=("INSERT INTO usertbl (fname,middeinitial,lname,contactnum,province,city,username,password,type) VALUES ('$firstname','$middleinitial','$lastname','$contactnumber','$province',
'$city','$username','$password','$type')");
if(mysqli_query($db, $query)){
echo "Inserted successfully";
}else{
echo "error:-".mysqli_error($db);
}
}
?>
Note:- your code is open to SQLInjection. So read about prepared statements of mysqli_* Or PDO and use them.

Add multiple inputs with same name to mysql database

I'm trying to fill my mysqli database from a form with multiple input fields with the same structure and names using ajax, but I'm pretty stuck here. I have tried several solutions but all with te same result, only my last two input fields are send to my database.
My form looks like this;
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[shortname]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[fullname]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[shortname]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[fullname]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
The ajax part;
$('#sendform').submit(function(e){
$.ajax({
type: 'POST',
url: 'inc/post.php',
data: $(this).serialize(),
success: function(data){
alert("Data Save: " + data);
}
});
e.preventDefault();
});
And my php;
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DBNAME', 'test');
$db = new mysqli(HOST, USER, PASS, DBNAME);
$values = array();
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'] . ',' . $member['fullname'] . ')';
}
$sql = "INSERT INTO sendform (shortname, fullname) VALUES " . implode(',', $values);
$result = $db->query($sql);
if($result) {
echo "Yep";
}
$db->close();
What am I doing wrong here? thnx!
Try this:
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[shortname][]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[fullname][]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[shortname][]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[fullname][]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
And PHP Page:
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DBNAME', 'test');
$db = new mysqli(HOST, USER, PASS, DBNAME);
$values = array();
for($i=0 ;$i < count($_POST['member']); $i++) {
$values[] = '("' . $_POST['member']['shortname'][$i] . '","' . $_POST['member']['fullname'][$i] . '")';
}
$sql = "INSERT INTO sendform (shortname, fullname) VALUES " . implode(',', $values);
$result = $db->query($sql);
if($result) {
echo "Yep";
}
$db->close();
You can make the form look like this (note that member[] is now an array itself)
<form id="sendform" method="post" action="">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[1][shortname]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[1][fullname]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[2][shortname]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[2][fullname]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
in your PHP use
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'] . ',' . $member['fullname'] . ')';
}
You are using member[shortname] and member[fullname] two times in html. So values from second fieldset are overriding the first one.
Hence we are getting only the values the are put in second fieldset.
You can try using following code:
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member['shortname'][0]" readonly />
<input type="text" value="John Doe" id="fullname" name="member['fullname'][0]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member['shortname'][1]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member['fullname'][1]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
And my php, make the following changes:
$values = array();
$i = 0;
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'][$i] . ',' . $member['fullname'][$i] . ')';
$i++;
}
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[date][]" required placeholder="Enter Date (yy-mm-dd)"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[msg][]" required placeholder="Enter Expenses Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[vendor][]" required placeholder="Vendor Name Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[amount][]" required placeholder="Enter Amount Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[prove][]" required placeholder="Prove"></td>
PHP Code
<?php
if(isset($_POST['submit'])){
//$date = mysql_real_escape_string($_POST['date']);
//$date = strtotime($date);
// $date = date('Y-m-d', $date);
//$msg = mysql_real_escape_string($_POST['msg']);
//$vendor = mysql_real_escape_string($_POST['vendor']);
//$amount = mysql_real_escape_string($_POST['amount']);
//$prove = mysql_real_escape_string($_POST['prove']);
$values = array();
for($i=0 ;$i < count($_POST['expense']); $i++) {
$values[] = '("' . $_POST['expense']['date'][$i] . '","' . $_POST['expense']['msg'][$i] . '","' . $_POST['expense']['vendor'][$i] . '","' . $_POST['expense']['amount'][$i] . '","' . $_POST['expense']['prove'][$i] . '")';
}
$sql = ("INSERT INTO expenses (date, msg, vendor, amount, prove) VALUES " . implode(',', $values));
echo "<script>alert('Your expenses data inserted successfully!')</script>";
echo "<script>window.open('expenses.php','_self')</script>";
exit();
}
?>
show me the error Notice: Undefiened offset:2 in ..............

my code wont update after search

can anyone help me find the code thats causing my code not to work? my code wont update... ive been debuging this code for 3hours already stil cant fix it :(...i need your help guys.
php code:
<?php
if(isset($_GET['gogo'])){
include('include/connect.php');
$batchcode = $_GET['code'];
$sql = mysql_query("SELECT * FROM score WHERE batchcode = '".$batchcode."' ");
if($sql) {
while($rows = mysql_fetch_array($sql)){
$id[] = $rows['id'];
$name[] = $rows['name'];
$score1[] = $rows['score1'];
$score2[] = $rows['score2'];
$other_qual[] = $rows['score3'];
$interview[] = $rows['score4'];
$total[] = $rows['total'];
}
}
}
?>
<?php
if(isset($_POST['update'])){
include('include/connect.php');
//1
$u1id = $_POST['id1'];
$u1name = $_POST['name1'];
$u1score1 = $_POST['optA1'];
$u1score2 = $_POST['optB1'];
$u1other_qual = $_POST['other_qual1'];
$u1interview = $_POST['interview1'];
$u1total = $_POST['total1'];
//2
$u2id = $_POST['id2'];
$u2name = $_POST['name2'];
$u2score1 = $_POST['optA2'];
$u2score2 = $_POST['optB2'];
$u2other_qual = $_POST['other_qual2'];
$u2interview = $_POST['interview2'];
$u2total = $_POST['total2'];
//1
mysql_query("UPDATE score SET score1='$u1score1', score2='$u1score2', total='$u1total' WHERE id='$u1id'");
//2
mysql_query("UPDATE score SET score1='$u2score1', score2='$u2score2', total='$u2total' WHERE id='$u2id'");
header("Location: index.php");
}
?>
html code:
<form method="get">
<form method="post">
Search batchcode: <input type="text" name="code" id="query" /><input type="submit" value="Go" name="gogo" /><br />
<table>
<tr>
<td>
ID: <br />
<input type="text" name="id1" value="<?php if(empty($id[0])){$id[0] = array(NULL);}else{echo $id[0];} ?>" readonly /> <br />
<input type="text" name="id2" value="<?php if(empty($id[1])){$id[1] = array(NULL);}else{echo $id[1];} ?>" readonly /> <br />
</td>
<td>
Name: <br />
<input type="text" name="name1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" readonly /> <br />
<input type="text" name="name2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" readonly /> <br />
</td>
<td>
Score 1: <br />
<input type="text" name="optA1" value="<?php if(empty($score1[0])){$score1[0] = array(NULL);}else{echo $score1[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optA2" value="<?php if(empty($score1[1])){$score1[1] = array(NULL);}else{echo $score1[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Score 2: <br />
<input type="text" name="optB1" value="<?php if(empty($score2[0])){$score2[0] = array(NULL);}else{echo $score2[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optB2" value="<?php if(empty($score2[1])){$score2[1] = array(NULL);}else{echo $score2[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Other Qualification: <br />
<input type="text" name="other_qual1" value="<?php if(empty($other_qual[0])){$other_qual[0] = array(NULL);}else{echo $other_qual[0];} ?>" readonly /> <br />
<input type="text" name="other_qual2" value="<?php if(empty($other_qual[1])){$other_qual[1] = array(NULL);}else{echo $other_qual[1];} ?>" readonly /> <br />
</td>
<td>
Interview: <br />
<input type="text" name="interview1" value="<?php if(empty($interview[0])){$interview[0] = array(NULL);}else{echo $interview[0];} ?>" readonly /> <br />
<input type="text" name="interview2" value="<?php if(empty($interview[1])){$interview[1] = array(NULL);}else{echo $interview[1];} ?>" readonly /> <br />
</td>
<td>
Total: <br />
<input type="text" name="total1" value="<?php if(empty($total[0])){$total[0] = array(NULL);}else{echo $total[0];} ?>" readonly onKeyUp="optTotal1()" /> <br />
<input type="text" name="total2" value="<?php if(empty($total[1])){$total[1] = array(NULL);}else{echo $total[1];} ?>" readonly onKeyUp="optTotal2()" /> <br />
</td>
</tr>
</table>
<input type="submit" value="update" name="update" />
</form>
</form>
You cannot do a GET and POST at the same time. Use one or the other..
In your HTML remove the <form method="get"> and the corresponding </form> and just use POST. (<form method="post">)
See this: Post and get at the same time in php
So then in your PHP, change GET to POST like so:
if(isset($_POST['gogo'])){
include('include/connect.php');
$batchcode = $_POST['code'];
$sql = mysql_query("SELECT * FROM score WHERE batchcode = '".$batchcode."' ");
...
EDIT:
Or alternatively, you could keep your php code the same the way you have it and just make it 2 seperate forms in your HTML,.. The search form using GET and the other form using POST
So HTML would be this:
<form method="get">
Search batchcode: <input type="text" name="code" id="query" /><input type="submit" value="Go" name="gogo" /><br />
</form>
<form method="post">
<table>
<tr>
<td>
ID: <br />
<input type="text" name="id1" value="<?php if(empty($id[0])){$id[0] = array(NULL);}else{echo $id[0];} ?>" readonly /> <br />
<input type="text" name="id2" value="<?php if(empty($id[1])){$id[1] = array(NULL);}else{echo $id[1];} ?>" readonly /> <br />
</td>...
...
</form>
If your code won't update then it is very likely you are updating the wrong files. Make sure that you are updating the files on the server or even better updating the files on your local drive and then uploading them to the server.
Check that you have permissions to upload to the correct place too. Might be that your uploads are failing because of bad permissions.

Can't submit form data to mysql

I cant get vars in php file. Echo returns nothing and I can't make a query.
Please help me. I know there is few stupid mistakes, but I'm really tired to search it..
I have a form:
<form align="center" method="POST" action="saveuser.php">
<small>Choose person:</small>
<select name="user" id="user" size="1" onchange="loadUserData()">
<option>Please select</option>
<?php
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Connection error: ' . mysql_error());
}
echo 'Connection Success';
mysql_select_db("live", $con);
$sql_query="SELECT name,id FROM res1";
if(!mysql_query($sql_query, $con))
{
die('INSERT Error: ' . mysql_error());
}
$query = mysql_query($sql_query, $con);
$max_row=0;
while ($row = mysql_fetch_assoc($query)){
if ($max_row !=0){
echo "<option value={$row[id]}>{$row[name]}</option>";
}
$max_row=1;
}
mysql_close($con);
?>
</select></br>
<input id="id" type="hidden"/>
<small>Name: </small><input id="name" type="text" size="40" />
<small>Nat: </small><input id="nat" type="text" size="30" />
<small>Licence: </small><input id="licence" type="text" size="10" /></br>
<small>R1: </small><input id="r1" type="text" size="5" />
<small>R2: </small><input id="r2" type="text" size="5" />
<small>R3: </small><input id="r3" type="text" size="5" />
<small>R4: </small><input id="r4" type="text" size="5" />
<small>R5: </small><input id="r5" type="text" size="5" />
<small>R6: </small><input id="r6" type="text" size="5" />
<small>R7: </small><input id="r7" type="text" size="5" /></br>
<small>FO1: </small><input id="f1" type="text" size="5" />
<small>FO2: </small><input id="f2" type="text" size="5" />
<small>FO3: </small><input id="f3" type="text" size="5" /></br>
<button type="submit" name="submit">Save</button>
And php file saveuser.php:
<?php
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Connection error: ' . mysql_error());
}
//echo 'Connection Success';
mysql_select_db("live", $con);
$id = $_POST["id"];
$name = $_POST["name"];
echo $id;
echo $name;
$sql_query="UPDATE res1 SET name='".$name."' WHERE id='".$id."'";
//,nat,licence,r1,r2,r3,r4,r5,r6,r7,f1,f2,f3 FROM res1 WHERE id='$value'";
$query = mysql_query($sql_query, $con);
mysql_close($con);
//$URL="edit.php";
//header ("Location: $URL");
?>
That is because you are not setting the name of the fields, id does not replace name:
<small>Name: </small><input name="name" id="name" type="text" size="40" />
<small>Nat: </small><input name="nat" id="nat" type="text" size="30" />
// etc ...
You have not given your inputs a name attribute.
Your $_POST array will only contain values for the ones that do.
try input submit
<input type="submit" value="Submit" />
then close the form
and give a name to the inputs

Categories