PHP. Update database with submit button?

Kendall

New member
Hi, I've been having trouble updating my database with a submit button. I've done a lot of searching and I have no idea why it isn't working. I can pass the values past the submit button but it will not store in the database.




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

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

<table width="100%" border="0" cellpadding="0" cellspacing="5">

<form name="update category" method="POST" action="edit.php">
<tr>
<td> </td>
<td><strong>ID Number:</strong></td>
</tr>
<tr>
<td> </td>
<td >

<?php

include 'studentinfo_connect.php';

// Connect to server and select database.
mysql_connect("$server", "$user", "$pass")or die("cannot connect");
mysql_select_db("$db")or die("cannot select DB");

$sql = mysql_query("SELECT ID FROM studentinfo_tbl ORDER BY ID");
$row = mysql_fetch_array($sql);
?>

<select name="ID">
<?php do{ ?>
<option value="<?php echo $row['ID']; ?>"><?php echo $row['ID']; ?> </option>
<?php } while($row = mysql_fetch_array($sql));?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" value="Search"></td>
</tr>
</table>




<?php
include('studentinfo_connect.php');
mysql_connect("$server", "$user", "$pass")or die("cannot connect");
mysql_select_db("$db")or die("cannot select DB");


//check if submit button was pressed
if(isset($_POST['submit']))
{
if (is_numeric($_POST['ID']))
{
$ID = $_POST['ID'];


$result = mysql_query("SELECT * FROM studentinfo_tbl WHERE ID=$ID")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
?>


<br /><label>ID Number: </label><input type="text" name="ID" value="<?php echo $row['ID']; ?>"/><br />
<br /><label>First Name: </label><input type="text" name="FN" value="<?php echo $row['FirstName']; ?>"/><br />
<br /><label>Last Name: </label><input type="text" name="LN" value="<?php echo $row['LastName']; ?>"/><br /><br />
<label>Year: </label>
<select name="Year" value="<?php echo $row['Year']; ?>"><br />
<br /><option value="1">First Year</option><br />
<br /><option value="2">Second Year</option><br />
<br /><option value="3">Third Year</option><br />
<br /><option value="4">Fourth Year</option><br />
</select><br />
<br /><label>Course: </label>
<select name="Course" value="<?php echo $row['Course']; ?>"><br />
<br /><option value="IT">BS Information Technology</option><br />
<br /><option value="HRM">BS Hotel and Restaurant Management</option><br />
<br /><option value="EDUC">BS Education</option><br />
</select><br />
<br /><input type="submit" value="UPDATE" name="update" /><br /><br />

<?php

include('studentinfo_connect.php');


if (isset($_POST['update']))
{

if (is_numeric($_POST['ID']))
{

$ID = $_POST['ID'];
$FN = mysql_real_escape_string(htmlspecialchars($_POST['FN']));
$LN = mysql_real_escape_string(htmlspecialchars($_POST['LN']));
$Year = mysql_real_escape_string(htmlspecialchars($_POST['Year']));
$Course = mysql_real_escape_string(htmlspecialchars($_POST['Course']));


if ($ID == '' || $FN == '' || $LN == '' || $Year == '' || $Course == '')
{

$error = 'ERROR: Please fill in all required fields!';


renderForm($ID, $FN, $LN, $Year, $Course, $error);
}
else
{

mysql_query("UPDATE studentinfo_tbl SET FirstName='$FN', LastName='$LN', Year='$Year', Course='$Course' WHERE ID='$ID'")
or die(mysql_error());


header("Location: view.ph
 
You would have a function such as "newthread()". You could set it up like this.

function newthread() {

if (isset($_POST['submit'])) {
$title = $_POST['title'];
$message = $_POST['content'];

//Query to update database with new thread
}
else {
//Show Form
}

So it will default to show the form when they click new thread. Once submitted the query will update the database and save the new thread.
 
Back
Top