Dont mind the security issues, this is just local testing, but when ever i click the update button none of the changes go through on the page or on the query and i get no erros.
<?php
$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");
if (isset($_POST['update'])) {
$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);
};
$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);
echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
?>
change your form to
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<input type=hidden name=update>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
POST keys should be in quotes. Try this instead:
$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id=".$_POST['id'];
Try this:
$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");
Also you can try echoing something inside your if (isset($_POST['update'])) { to make sure it is testing true.
Here is your problem:
if (isset($_POST['submit']) && $_POST['submit'] == 'update') {
The name of the submit button is submit not update, the value is update.
Related
My <input> values does not get post to my database when I click the submit button. I am unable to find the error. Please help.
<?php
echo "<form action=Display.php method=post>";
echo "<td>" . $fiberexcel['Engineer9'] ." </td>";
echo "<td>" . $fiberexcel['AM10'] ." </td>";
echo "<td>" . "<input type=date name=A12 value=" . $fiberexcel['Quotation11'] ." </td>";
$A6 = date("Y-m-d");
$ExpectDateQuotation12 = strtotime($ReqDate4."+ 10 weekday");
echo "<td>" . date("Y-m-d",$ExpectDateQuotation12) . "</td>";
$ExpectDateQuotation12 = date("Y-m-d",$ExpectDateQuotation12);
$UpdateQuery = "UPDATE `fiberexcel` SET `ExpectDateQuotation12` =
'$ExpectDateQuotation12' WHERE `fiberexcel`.`SiteID0` = '$A1';";
mysqli_query($conn, $UpdateQuery);
echo "<td>" . "<input type=date name=A14 value=" . $fiberexcel['ApprovalJFSRequest13'] ." </td>";
?>
echo "<td>" . "<input type=hidden name=hidden value=" .$fiberexcel['SiteID0'] ." </td>";
//echo "</tr>";
echo "<td>" . "<input type=submit name=update value=update>". "</td>";
echo "</form>";
This is my update button post function:
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE `fiber`.`fiberexcel` SET Quotation11='$_POST[A12]' , ApprovalJFSRequest13='$_POST[A14]' WHERE `fiberexcel`.`SiteID0`='$_POST[hidden]'";
mysqli_query($conn, $UpdateQuery);
};
Could you please check your HTML output in browser.
I have noticed some issues in your code
-you have missed quotes for every attribute values
ex:
echo "";
wolud be like
echo "";
also there is an error in your HTML code,
ex:
echo "" . "";
here your have missed to close the input tag,
your code wold be like below,
<?php
echo "<form action='Display.php' method='post'>";
echo "<td>" . $fiberexcel['Engineer9'] ." </td>";
echo "<td>" . $fiberexcel['AM10'] ." </td>";
echo "<td>" . "<input type='date' name='A12' value='" . $fiberexcel['Quotation11'] ."'></td>";
$A6 = date("Y-m-d");
$ExpectDateQuotation12 = strtotime($ReqDate4."+ 10 weekday");
echo "<td>" . date("Y-m-d",$ExpectDateQuotation12) . "</td>";
$ExpectDateQuotation12 = date("Y-m-d",$ExpectDateQuotation12);
$UpdateQuery = "UPDATE `fiberexcel` SET `ExpectDateQuotation12` =
'$ExpectDateQuotation12' WHERE `fiberexcel`.`SiteID0` = '$A1';";
mysqli_query($conn, $UpdateQuery);
echo "<td>" . "<input type='date' name='A14' value='" . $fiberexcel['ApprovalJFSRequest13'] ."'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" .$fiberexcel['SiteID0'] ."'> </td>";
//echo "</tr>";
echo "<td>" . "<input type='submit' name='update' value='update'>". "</td>";
echo "</form>";
?>
There is some syntax error in your code. Please check the php close tag and input close tag. Try this..
<?php
echo "<form action='Display.php' method='post'>";
echo "<td>" . $fiberexcel['Engineer9'] ." </td>";
echo "<td>" . $fiberexcel['AM10'] ." </td>";
echo "<td><input type='date' name='A12' value=" . $fiberexcel['Quotation11'] ."/></td>";
$A6 = date("Y-m-d");
$ExpectDateQuotation12 = strtotime($ReqDate4."+ 10 weekday");
echo "<td>" . date("Y-m-d",$ExpectDateQuotation12) . "</td>";
$ExpectDateQuotation12 = date("Y-m-d",$ExpectDateQuotation12);
$UpdateQuery = "UPDATE `fiberexcel` SET `ExpectDateQuotation12` =
'$ExpectDateQuotation12' WHERE `fiberexcel`.`SiteID0` = '$A1';";
mysqli_query($conn, $UpdateQuery);
echo "<td><input type='date' name='A14' value=" . $fiberexcel['ApprovalJFSRequest13'] ." /></td>";
echo "<td><input type='hidden' name='hidden' value=" .$fiberexcel['SiteID0'] ." /></td>";
//echo "</tr>";
echo "<td><input type='submit' name='update' value='update'></td>";
echo "</form>";
?>
these are the codes
where do you think I should insert the header (Location:'home.php')
I tried using after the if statements, these are the codes
else {
try {
header('Location: home.php');
}
but it directly goes to home.php even when I haven't clicked the update button
<?php
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE registry SET db_id = '$_POST[db_id]', db_name =
'$_POST[db_name]', db_age = '$_POST[db_age]', db_gender =
'$_POST[db_gender]', db_birthdate = '$_POST[db_birthdate]', db_phone =
'$_POST[db_phone]', db_address = '$_POST[db_address]'
WHERE db_id = '$_POST[hidden]'";
mysql_query($UpdateQuery);
}
$sql = "SELECT * FROM registry";
$query = mysql_query($sql) or throw_ex(mysql_error()); ;
echo"<h2><center>Employee Masterlist</center></h2>";
echo "<table class = \"form\" border = \"1\" cellspacing = \"3\">";
echo "<tr>";
echo "<th>Employee ID</th>";
echo "<th>Name</th>";
echo "<th>Age</th>";
echo "<th>Gender</th>";
echo "<th>Birthdate </th>";
echo "<th>Phone No. </th>";
echo "<th>Address </th>";
echo "</tr>";
while($row = mysql_fetch_array($query))
{
echo "<form action=update.php method=post>";
echo "<tr>";
echo "<td>" . "<input id=input-up type=text name=db_id
value=" . $row['db_id'] . "> </td>";
echo "<td>" . "<input id=input-up type=text name=db_name
value=" . $row['db_name'] . "> </td>";
echo "<td>" . "<input id=input-up type=text name=db_age
value=" . $row['db_age'] . "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_gender value=" . $row['db_gender']. "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_birthdate value=" . $row['db_birthdate'] . "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_phone value=" . $row['db_phone'] . "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_address value=" . $row['db_address'] . "> </td>";
echo "<input type=hidden name=hidden value=" .
$row['db_id'] . "> </td>";
echo "<td>" . "<input class=send_btn type=submit
name=update value=Update". "> </td>";
echo "</tr>";
echo "</form>";
}
echo"</table>";
mysql_close();
?>
</div>
</body>
</html>
You should add it after query is executed.
Here is the code
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE registry SET db_id = '$_POST[db_id]', db_name =
'$_POST[db_name]', db_age = '$_POST[db_age]', db_gender =
'$_POST[db_gender]', db_birthdate = '$_POST[db_birthdate]', db_phone =
'$_POST[db_phone]', db_address = '$_POST[db_address]'
WHERE db_id = '$_POST[hidden]'";
mysql_query($UpdateQuery);
//redirect to home.php
header('Location: home.php');
}
I'm trying to is have have table to display my database with an update, delete, and add functions.
My problem is when I view it. It doesn't show the full text in the "Description" column and also in "Developer". Also in the "SecondDirectory" column it shows </td instead of just blank if its blank.
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("$$$$","$$$$","$$$$");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mydb",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE table SE ID='$_POST[topic]',Filename='$_POST[filename]', Description='$_POST[description]', TopDirectory='$_POST[topdirectory]', SecondDirectory='$_POST[seconddirectory]', Developer='$_POST[developer]', Date='$_POST[date]' WHERE ID='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM table WHERE ID='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO table (ID,Filename,Description,TopDirectory,SecondDirectory,Developer,Date) VALUES ('$_POST[uid]','$_POST[ufilename]','$_POST[udescription]','$_POST[utopdirectory]','$_POST[useconddirectory]','$_POST[udeveloper]','$_POST[udate]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM table";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Filename</th>
<th>Description</th>
<th>TopDirectory</th>
<th>SecondDirectory</th>
<th>Developer</th>
<th>Date</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=theworks.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=id value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=text name=filename value=" . $record['Filename'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
echo "<td>" . "<input type=text name=topdirectory value=" . $record['TopDirectory'] . " </td>";
echo "<td>" . "<input type=text name=seconddirectory value=" . $record['SecondDirectory'] . " </td>";
echo "<td>" . "<input type=text name=developer value=" . $record['Developer'] . " </td>";
echo "<td>" . "<input type=text name=date value=" . $record['Date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=database.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=text name=ufilename></td>";
echo "<td><input type=text name=udescription><td>";
echo "<td><input type=text name=utopdirectory></td>";
echo "<td><input type=text name=useconddirectory></td>";
echo "<td><input type=text name=udeveloper><td>";
echo "<td><input type=text name=udate></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
So I figured it out.
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
What I needed to put is a single quotation right after value= and two single quotations in " " so that it also counts the spaces. The code should have been...
echo "<td>" . "<input type=text name=description value='" . $record['Description'] . "'' </td>";
I have a form created with php to edit records in a MySQL db. I would like to add a drop down menu to this form for the "type" field but am not sure how to create it without losing the data already in the db field.
<?php
require_once '../php/dbconfig.php';
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['update'])) {
$UpdateQuery = "UPDATE members SET name='$_POST[name]', type='$_POST[type]', physicaladdress='$_POST[physicaladdress]', billingaddress='$_POST[billingaddress]', contact='$_POST[contact]', phone='$_POST[phone]', fax='$_POST[fax]', email='$_POST[email]', web='$_POST[web]', description='$_POST[description]' WHERE id='$_POST[id]'";
mysqli_query($conn, $UpdateQuery);
}
$result = mysqli_query($conn, "SELECT * FROM members ORDER BY name");
echo "<br />";
echo "<table border=0>
<tr>
<th>Name</th>
<th>Type</th>
<th>Physical Address</th>
<th>Billing Address</th>
<th>Contact Name</th>
<th>Phone</th>
<th>Fax</th>
<th>Email</th>
<th>Web</th>
<th>Description</th>
</tr>" ;
while($record = mysqli_fetch_array($result)) {
echo "<form action = admin-update.php method = post>";
echo "<tr>";
echo "<td>" . "<input type=text size=42 name=name value='" . $record['name'] . "' </td>";
echo "<td>" . "<input type=text size=30 name=type value='" . $record['type'] . "' </td>";
echo "<td>" . "<input type=text size=60 name=physicaladdress value='" . $record['physicaladdress'] . "' </td>";
echo "<td>" . "<input type=text size=60 name=billingaddress value='" . $record['billingaddress'] . "' </td>";
echo "<td>" . "<input type=text size=20 name=contact value='" . $record['contact'] . "' </td>";
echo "<td>" . "<input type=text size=10 name=phone value='" . $record['phone'] . "' </td>";
echo "<td>" . "<input type=text size=10 name=fax value='" . $record['fax'] . "' </td>";
echo "<td>" . "<input type=text size=25 name=email value='" . $record['email'] . "' </td>";
echo "<td>" . "<input type=text size=25 name=web value='" . $record['web'] . "' </td>";
echo "<td>" . "<input type=text 50 name=description value='" . $record['description'] . "' </td>";
echo "<td>" . "<input type=hidden name=id value='" . $record['id'] . "' </td>";
echo "<td>" . "<input type=submit name= update value=Update" . " </td>";
echo "</form>";
}
echo "</table>";
$conn->close();
?>
Any assistance would be greatly appreciated. Thank you.
You should be able to change the type from an input to a select with options and include the value from the database as one of the options.
I suggest this:
$selectType = '<select name="type">';
$selectType .= '<option value="'. $record['type'] .'">"'. $record['type'] .'"</option>';
$selectType .= '<option value="Opt1">Option1</option>';
$selectType .= '<option value="Opt2">Option2</option>';
$selectType .= '<option value="Opt3">Option3</option>';
$selectType .= '</select>;
Then, inside your while loop:
echo "<td>" . $selectType . "</td>";
I hope this works for you. Please let me know if it does not or if it does not answer your question.
so I have this code that other Stack members have helped me fine tune and correct some errors, the code all works as it should but there is one small detail, after successfully editing one record and clicking the update button ALL of the existing records that are in the database load into the page. Here is my code below:
<?php
$con = mysql_connect("localhost", "root", "M1q2w3e4r");
if (!$con) {
die("Can not connect: " . mysql_error());
}
mysql_select_db("inventory",$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
};
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
$sql = "SELECT * FROM invoice".$where;
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Inv #</th>
<th>From</th>
<th>To</th>
<th>Type</th>
<th>Notes</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action='edit.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='inv_number' value='" . $record['inv_number'] . "'> </td>";
echo "<td>" . "<input type='text' id='from' name='from_date' value='" . $record['from_date'] . "'> </td>";
echo "<td>" . "<input type='text' id='to' name='to_date' value='" . $record['to_date'] . "'> </td>";
echo "<td>" . "<input type='text' name='date_type' value='" . $record['date_type'] . "'> </td>";
echo "<td>" . "<input type='text' name='notes' value='" . $record['notes'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='id' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close($con);
?>
I know it has to do with the form action="edit.php", as it refreshes the page the id number in the url is pulled out. So I tried this:
echo "<form action='edit.php?edit=<?php echo $_REQUEST["id"]; ?>' method='post'>";
but this only led to my edit.php to display as a blank page. If anyone can help me figure out how to prevent all the database records from being displayed in the page after clicking the update button it would really help.
I might do this, for example, if I just wanted to show the record that was just updated:
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
$where = ' where id='.$_POST[id];
}
else {
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
}
You could also use REQUEST instead of GET and make a hidden input field with the name "edit" in your form.