Simple update to Mysql

I am a complete newbie to php and mysql. I am just trying to achieve something simple right now as a test.

I have a Mysql database with 3 columns (Chapter, Editor, Approved). Using form I have managed to get these to show on a page to allow someone to change the relevant details then submit them. The problem is when the page updates it shows the old details - ie the changes are not stored. I am sure I am doing something incredibly stupid but after 2 hours can’t see it. I’m sure someone with some knowledge will spot it in a heartbeat. The necessary code.

The page to show the info
<?php
include_once ‘includes/dbh_progress.inc.php’;
?>

<html>
<head>
<title>Update progress</title>
</head>
<body>

<?php
//connect to dbase
//done in inc file

//select database - in inc file?
//mysqli_select_db($conn,'progress');

//select query
$sql="SELECT * FROM VM;";

//execute query
$records=mysqli_query($conn,$sql);



?>
<table>
<tr>
	<th>Chapter</th>
	<th>Staff</th>
	<th>Approved for submission</th>
	
</tr>
<?php
while($row=mysqli_fetch_array($records))
	{
	echo "<tr><form action=update.php method=post>";
	echo "<td><input type=text name=chapter_i value='".$row['Chapter']."'</td>";
	echo "<td><input type=text name=editor_i value='".$row['Editor']."'</td>";
	echo "<td><input type=date name=submission_i value='".$row['Approved']."'</td>";
	echo "<td><input type=submit>";
	echo "</form></tr>";
}
?>
</table>
</body>
</html>

update.php
<?php

include_once 'includes/dbh_progress.inc.php';

//connect to dbase
//done in inc file

//select database - in inc file?
//mysqli_select_db($conn,'progress');

//update query
$sql="update VM set Approved='$submission_i' WHERE Chapter='$Chapter'";
//can use comma and another for each variable


//execute query
if(mysqli_query($conn,$sql))
	header("refresh:1; url=test_update.php");
else
	echo "NO";


?>

The primary key is chapter. If you need anything more please yell. TIA

Yes, the problem is very obvious. Where do you expect the values for those “magic” update variables to come from? They just appear out of nowhere don’t they?

Before you spend too much time learning Mysqli, I would suggest you just stop and learn PDO with Prepared Statements instead. This will get you going https://phpdelusions.net/pdo

You can also download my PDO Bumpstart Database that will help you get going. https://github.com/benanamen/pdo_bumpstart_ver1.6

I don’t quite follow. Aren’t the update variable submission_i being the name attached to that box on the form? Or am I missing something (yet again!)?

Yes, but the values are held in the POST array. PHP does not magically (not anymore) create variables. So you would access the values like $_POST[‘submission_i’]

That’s what I originally had before I took it out. STuck it back but still doesn’t work.

This is what I have at the moment (after putting POST back).

$sql="update VM set Approved='$_POST[submission_i]' WHERE Chapter='$_POST[Chapter]'";
//can use comma and another for each variable
mysqli_query($conn,$sql);

For one, that’s not what I showed you. Look at my post again. Naturally you looked at the error logs right? What does the error log say? And of course you have error reporting turned on right?

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service