Table will not update PHP - php

I get the id from the page before. Everything on this page gets populated from the database just fine. when i hit add button the database does not get populated. Everything looks good to me but i can no figure out why it wont update. I am new to php. I am sure my code is very sloppy
<?php
{
$Reg_ID = $_POST['id'];
$dbhost = '';
$dbuser = '';
$dbpass = '';
$database ='';
$table = '';
if(isset($_POST['add']))
{
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$Reg_F_Name = addslashes ($_POST['Reg_F_Name']);
$Reg_L_Name = addslashes ($_POST['Reg_L_Name']);
}
else
{
$Req_F_Name = $_POST["Req_F_Name"];
$Reg_L_Name = $_POST["Reg_L_Name"];
}
$Req_F_Name = $_POST["Req_F_Name"];
$Reg_L_Name = $_POST["Reg_L_Name"];
$Reg_Phone = $_POST["Reg_Phone"];
$Reg_Email = $_POST["Reg_Email"];
$Reg_Mod_Request = $_POST["Reg_Mod_Request"];
$Reg_Address_1 = $_POST["Reg_Address_1"];
$Reg_Address_2 = $_POST["Reg_Address_2"];
$Reg_City = $_POST["Reg_City"];
$Reg_State = $_POST["Reg_State"];
$Reg_Zip_Code= $_POST["Reg_Zip_Code"];
$Reg_ID= $_POST["Reg_ID"];
$Reg_Phone= str_replace("-","","$Reg_Phone");
$sql = "UPDATE $table".
"(Reg_F_Name,Reg_L_Name, Reg_Phone, Reg_Email, Reg_Mod_Request, Reg_Address_1, Reg_Address_2, Reg_City, Reg_State, Reg_Zip_Code) ".
"VALUES('$Reg_F_Name','$Reg_L_Name','$Reg_Phone','$Reg_Email','$Reg_Mod_Request','$Reg_Address_1','$Reg_Address_2','$Reg_City','$Reg_State','$Reg_Zip_Code')".
"WHERE Reg_ID = '$Reg_ID'";
mysql_select_db($database);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<?php
$con=mysqli_connect($dbhost, $dbuser, $dbpass, $database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = mysqli_query($con, "SELECT * FROM Request WHERE Reg_ID = '$Reg_ID'");
?>
<form method="post" action="viewrequests.php" style="width: 500px">
<fieldset>
<input type='hidden' name='__token_timestamp__' value='1397526990'>
<input type='hidden' name='__token_val__' value='34a10d1cfc4b20e45c901e83624677ad'>
<p style="text-align: center">Update Prayer Request</p>
<div style="width: 500px; float: left">
<?php
while($rows = mysqli_fetch_array($query))
{
?>
Please pray for:
<br />First Name: <input name="Reg_F_Name" type="text" id="Reg_F_Name" value="<? echo $rows['Reg_F_Name']; ?>">
<br />Last Name: <input name="Reg_L_Name" type="text" id="Reg_L_Name" value="<? echo $rows['Reg_L_Name']; ?>">
<br />Prayer Request: <? echo $rows['Reg_Request']; ?>
<br />Update Prayer Request:
<br /><textarea name="Reg_Mod_Request" type="varchar" id="Reg_Mod_Request" rows="5" cols="30"><? echo $rows['Reg_Request']; ?></textarea>
<br />Primary Address: <input name="Reg_Address_1" type="varchar" id="Reg_Address_1" value="<? echo $rows['Reg_Address_1']; ?>">
<br />Secondary Address:<input name="Reg_Address_2" type="varchar" id="Reg_Address_2" value="<? echo $rows['Reg_Address_2']; ?>">
<br />City:<input name="Reg_City" type="char" id="Reg_City" value="<? echo $rows['Reg_City']; ?>">
<br />State:<input name="Reg_State" type="char" id="Reg_State" value="<? echo $rows['Reg_State']; ?>">
<br />Zip:<input name="Reg_Zip_Code" type="char" id="Reg_Zip_Code" value="<? echo $rows['Reg_Zip_Code']; ?>">
<br />Phone Number (555-555-5555):<input name="Reg_Phone" type="char" id="Reg_Phone" value="<? echo $rows['Reg_Phone']; ?>">
<br />Email Address:<input name="Reg_Email" type="varchar" id="Reg_Email" value="<? echo $rows['Reg_Email']; ?>">
<br /><br />
</div>
<input name="add" type="submit" id="add" value="Update Prayer Request">
</fieldset>
</form>
<?php
}
}
mysql_close();
}
?>
I think the problem has something to do with
$sql = "UPDATE $table".
"(Reg_F_Name,Reg_L_Name, Reg_Phone, Reg_Email, Reg_Mod_Request, Reg_Address_1, Reg_Address_2, Reg_City, Reg_State, Reg_Zip_Code) ".
"VALUES('$Reg_F_Name','$Reg_L_Name','$Reg_Phone','$Reg_Email','$Reg_Mod_Request','$Reg_Address_1','$Reg_Address_2','$Reg_City','$Reg_State','$Reg_Zip_Code')".
"WHERE Reg_ID = '$Reg_ID'";
But i am not sure. Any help would be greatly appreciated.
echo $sql output
UPDATE Request (Reg_F_Name,Reg_L_Name, Reg_Phone, Reg_Email, Reg_Mod_Request, Reg_Address_1,
Reg_Address_2, Reg_City, Reg_State, Reg_Zip_Code) VALUES('joe','qwea','4055554321',
'Fell off windmill. Broken legs possibly going to l','Fell off windmill.',
'4059 Mt Lee Dr','','Altus','OK','73521')
WHERE Reg_ID = ''Could not enter data: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax to use near
'(Reg_F_Name,Reg_L_Name, Reg_Phone, Reg_Email, Reg_Mod_Request, Reg_Address_1, Re' at line 1

you need to put regid in a hidden field
<input type="hidden" name="Reg_ID" value="<?=$row['Reg_ID']?>">
so now you'll get this values in $_POST['Reg_ID']
or try like this
"UPDATE tablename".
"SET Reg_F_Name ='{$Reg_F_Name}',Reg_L_Name='{$Reg_L_Name}', Reg_Phone='{$Reg_Phone}', Reg_Email='{$Reg_Email}', Reg_Mod_Request='{$Reg_Mod_Request}', Reg_Address_1='{$Reg_Address_1}', Reg_Address_2='{$Reg_Address_2}', Reg_City='{$Reg_City}', Reg_State='{$Reg_State'}, Reg_Zip_Code='{$Reg_Zip_Code}' ".
"WHERE Reg_ID = '{$Reg_ID}'";

You are mixing INSERT syntax http://dev.mysql.com/doc/refman/5.6/en/insert.html
INSERT INTO tbl (columns) VALUES (values)
with UPDATE syntax http://dev.mysql.com/doc/refman/5.0/en/update.html
UPDATE tbl SET column=value WHERE column=value
Try something like
UPDATE $table SET
Reg_F_Name = '$Reg_F_Name',
Reg_L_Name = '$Reg_L_Name',
Reg_Phone = '$Reg_Phone',
Reg_Email = '$Reg_Email',
Reg_Mod_Request = '$Reg_Mod_Request',
Reg_Address_1 = '$Reg_Address_1',
Reg_Address_2 = '$Reg_Address_2',
Reg_City = '$Reg_City',
Reg_State = '$Reg_State',
Reg_Zip_Code = '$Reg_Zip_Code'
WHERE Reg_ID = '$Reg_ID'
Also, it looks like $Reg_ID is not set since you have
WHERE Reg_ID = ''
in your echo'ed sql. Add it as a hidden element to your form, so it is reset on form submit
<input type='hidden' name='id' value='<?php echo $Reg_ID; ?>'>

Related

PHP Header 302 Found

I am trying to go back to the page i came from after the update is submitted.
the $pagefrom gets populated from the page before. that works fine. I get redirected to a page that says 302 found but its the same url as my current page.
<?php
{
$pagefrom= $_POST['pagename'];
echo $pagefrom;
if(isset($_POST['add']))
{
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$Reg_F_Name = addslashes ($_POST['Reg_F_Name']);
$Reg_L_Name = addslashes ($_POST['Reg_L_Name']);
}
else
{
$Req_F_Name = $_POST["Req_F_Name"];
$Reg_L_Name = $_POST["Reg_L_Name"];
}
$Req_ID = $_POST["Req_ID"];
$Req_F_Name = $_POST["Req_F_Name"];
$Reg_L_Name = $_POST["Reg_L_Name"];
$Reg_Email = $_POST["Reg_Email"];
$Reg_Mod_Request = $_POST["Reg_Mod_Request"];
$Reg_Address_1 = $_POST["Reg_Address_1"];
$Reg_Address_2 = $_POST["Reg_Address_2"];
$Reg_City = $_POST["Reg_City"];
$Reg_State = $_POST["Reg_State"];
$Reg_Zip_Code= $_POST["Reg_Zip_Code"];
$Reg_ID= $_POST["Reg_ID"];
$Reg_Phone = $_POST["Reg_Phone"];
$Reg_Phone= str_replace("-","","$Reg_Phone");
$Reg_Approval_Status= $_POST["Reg_Approval_Status"];
$Reg_Status= $_POST["Reg_Status"];
$sql= "UPDATE $dbtable SET
Reg_F_Name = '$Reg_F_Name',
Reg_L_Name = '$Reg_L_Name',
Reg_Phone = '$Reg_Phone',
Reg_Email = '$Reg_Email',
Reg_Mod_Request = '$Reg_Mod_Request',
Reg_Address_1 = '$Reg_Address_1',
Reg_Address_2 = '$Reg_Address_2',
Reg_City = '$Reg_City',
Reg_State = '$Reg_State',
Reg_Zip_Code = '$Reg_Zip_Code',
Reg_Approval_Status='$Reg_Approval_Status',
Reg_Status='$Reg_Status'
WHERE Reg_ID = '$Reg_ID'";
mysql_select_db($database);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
mysql_close($conn);
header('Location: '.$pagefrom);
}
else
{
?>
<?php
$con=mysqli_connect($dbhost, $dbuser, $dbpass, $database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = mysqli_query($con, "SELECT * FROM $dbtable WHERE Reg_ID = '$Reg_ID'");
?>
<form method="post" action="" style="width: 500px">
<fieldset>
<p style="text-align: center">Update A Prayer Request</p>
<div style="width: 500px; float: left">
<?php
while($rows = mysqli_fetch_array($query))
{
?>
<input type="hidden" name="Reg_ID" value="<?=$rows['Reg_ID']?>">
Please pray for:
<br />First Name: <input name="Reg_F_Name" type="text" id="Reg_F_Name" value="<? echo $rows['Reg_F_Name']; ?>">
<br />Last Name: <input name="Reg_L_Name" type="text" id="Reg_L_Name" value="<? echo $rows['Reg_L_Name']; ?>">
<br />Original Prayer Request:
<br/><? echo $rows['Reg_Request']; ?>
<br />Update Prayer Request:
<br /><textarea name="Reg_Mod_Request" type="varchar" id="Reg_Mod_Request" rows="5" cols="30"><? echo $rows['Reg_Mod_Request']; ?></textarea>
<br />Primary Address: <input name="Reg_Address_1" type="varchar" id="Reg_Address_1" value="<? echo $rows['Reg_Address_1']; ?>">
<br />Secondary Address:<input name="Reg_Address_2" type="varchar" id="Reg_Address_2" value="<? echo $rows['Reg_Address_2']; ?>">
<br />City:<input name="Reg_City" type="char" id="Reg_City" value="<? echo $rows['Reg_City']; ?>">
<br />State:<input name="Reg_State" type="char" id="Reg_State" value="<? echo $rows['Reg_State']; ?>">
<br />Zip:<input name="Reg_Zip_Code" type="char" id="Reg_Zip_Code" value="<? echo $rows['Reg_Zip_Code']; ?>">
<br />Phone Number (555-555-5555):<input name="Reg_Phone" type="char" id="Reg_Phone" value="<? echo $rows['Reg_Phone']; ?>">
<br />Email Address:<input name="Reg_Email" type="varchar" id="Reg_Email" value="<? echo $rows['Reg_Email']; ?>">
<br />Approval Status: <select id="Approval" name="Approval">
<!--Call run() function-->
<option value="0" <?php echo $rows['Reg_Approval_Status'] == "0" ? "selected" : '' ?>>Waiting Approval</option>
<option value="1" <?php echo $rows['Reg_Approval_Status'] == "1" ? "selected" : '' ?>>Accept</option>
<option value="2" <?php echo $rows['Reg_Approval_Status'] == "2" ? "selected" : '' ?>>Decline</option>
</select>
<br />Enabled Request: <select id="Activate" name="Activate">
<option value="0" <?php echo $rows['Reg_Status'] == "0" ? "selected" : '' ?>>Disable</option>
<option value="1" <?php echo $rows['Reg_Status'] == "1" ? "selected" : '' ?>>Enable</option>
</select>
</div>
<input name="add" type="submit" id="add" value="Update Prayer Request">
</fieldset>
</form>
<?php
}
}
mysql_close();
}
?>
I think something is wrong with
header('Location: '.$pagefrom);
but everything i try i still can not get it to reload the page before.
Remove echo $pagefrom; from the top of your script or move the header line above it. Headers can not be sent after output has started.
See: header
Try adding die(); the next line you use Header('Location: ....'); - if you don't the php will continue working further.
Also you probably want to use the form in the page the source code is from. Here you dont provide the pagename anymore therefore when you use form from this page - the $pagefrom is empty and redirection goes to current page.
Try adding it as hidden input for this form, and it should work.

How to use forms to update SQL

I'm trying to make a form that uses a drop down, radio buttons, text field, textarea, and a hidden value(the time) then takes that information from that form and updates SQL database.
My form is below and it all loads correctly but I'm having issues updating the values and trying to figure out how to make the radio buttons and dropdowns to work since I can't make the value php code and need to pass the value. Everything I'm finding on the web is how to do text fields where the user types something.
When I select update it just submits the data but nothing changes. On my update.php I have a sanitize function at the very end and am unsure how to pass the variables in. Do I create an array named $var and input all my variables into it or pass each variable at a time?
I've been searching the web for HOW TO's and am currently reading two books but they don't go into enough detail so thanks for any assistance.
control.php
<?php
session_start();
if( !isset($_SESSION['myusername']) ){ header("Location: login.php"); }
?>
<?php
require("../../system/templates/includes/constants.php");
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if(!$connection) { die("Database connection failed: " .mysql_error()); }
$db_select = mysql_select_db(DB_NAME,$connection);
if(!$db_select) { die("Database selection failed: " . mysql_error()); }
?>
<form method="post" action="update.php">
<select name="name" required="true" value="<?php echo $row['name']; ?>">
<?php
$query="SELECT id, name FROM modules";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
echo "<option value=\"" . $row['id'] . "\">" . $row['name'] . "</option>";
}
?>
</select>
<br />
Select Status:
Red <input type="radio" value="red" name="status" />
Yellow <input type="radio" value="yellow" name="status" />
Green <input type="radio" checked="checked" value="green" name="status" />
<br />
Reason:
<br />
<select name="reason" required="true">
<option value="0" selected="selected" value="">Select Reason</option>
<option value="ONLINE">Online</option>
<option value="MAINTENANCE">Maintenance</option>
<option value="ERROR">Error</option>
<option value="OFFLINE">Offline</option>
<option value="">No Reason</option>
</select>
<br />
ETA:
<br />
<input type="text" name="eta" value="<?php echo $row['eta']; ?>" maxlength="8" />
<br />
Description:
<br />
<textarea rows="5" cols="30" name="explanation" wrap="hard" required="true" maxlength="320" value="<?php echo $row['description']; ?>" /></textarea>
<br />
<div align="right">
<input name="update" type="submit" value="Update"/>
<input type="hidden" name="last_updated" value="<?php $mysqldate = date ('H:i'); $phpdate = strtotime ( $mysqldate );?> />
</form>
update.php
<?php
print_r(_POST);
if(isset($POST['update']))
{
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if(! $connection)
{
die('Could not connect: ' .mysql_error());
}
$name = $POST['name'];
$status = $POST['status'];
$reason = $POST['reason'];
$eta = $POST['eta'];
$description = $POST['description'];
$last_updated = $POST['last_updated'];
$updated_by = $POST['updated_by'];
$sql = "UPDATE module SET status = $status , reason = $reason , eta = $eta , description = $description , last_updated = $last_updated , updated_by = $updated_by WHERE name = $name";
mysql_select_db('status');
$retval = mysql_query ( $sql, $connection);
if (!retval)
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully";
mysql_close($connection);
} else {
// not sure what to do here
}
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
function sanitizeMySQL($var)
{
$var = mysql_real_escape_string($var);
$var = satnizeString($var);
return $var;
}
header("Location: control.php");
?>
As always I greatly appreciate any assistance anyone can offer. I'm still in the very early stages of learning this and this website and community has helped me more than any book/tutorial I've read so far.
Your SQL statement needs quotation marks for each parameter.
$sql = "UPDATE module SET status = '$status' , reason = '$reason' , eta = '$eta' , description = '$description' , last_updated = '$last_updated' , updated_by = '$updated_by' WHERE name = '$name' ";
As for the sanitizeString() function, it only takes in one string at a time. Maybe something like the one below may be simple and clean:
$params = array($name, $status, $reason); // put all your params in here
foreach ($params as &$p) { // the '&' before $p is essential, so do not forget it
$p = sanitizeString($p);
}
Hope it helps.

PHP Form update will not update

I have written an Edit part of my Form but somehow it will not Update the edited Fields. The Code does not give any Errors but it will not update?!
If possible could somebody take a look please?
<?php
include "connect.php";//database connection
if (isset($_GET["id"])) {
$id = intval($_GET["id"]);
if (isset($_POST["edited"]))
{
$update = "UPDATE traumprojekt SET";
$update .= sprintf("quantityProduct='%s', " , mysql_real_escape_string($_POST["quantityProduct"]));
$update .= sprintf("titleProduct='%s', " , mysql_real_escape_string($_POST["titleProduct"]));
$update .= sprintf("informationProduct='%s'", mysql_real_escape_string($_POST["informationProduct"]));
$update .= "WHERE id = '$id'";
mysql_query($update);
}
$sql = "SELECT * FROM `traumprojekt` WHERE id=$id";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) == 1)
{
$row = mysql_fetch_assoc($res);
?>
<form method="post" action="edit_form.php?id=<?php echo $row["id"] ?>">
ID: <?php echo $row["id"] ?><br />
Quantity: <input type="text" name="quantityProduct" value="<?php echo $row["quantityProduct"] ?>"><br />
Product Title: <input type="text" name="titleProduct" value="<?php echo $row["titleProduct"] ?>"><br />
Product Information: <input type="text" name="informationProduct" value="<?php echo $row["informationProduct"] ?>"><br />
<input type="submit" name="submit" value="Update"><br />
<input type="hidden" name="edited" value="1">
</form>
<?php
}
}
?>

having trouble with mysql UPDATE

I have a submit button on the page submitting and it comes back blank and does not update , the echo is also blank.
<?
if (isset($_POST['submit'])) {
$newtext = mysql_real_escape_string($_POST['text']);
$doTextEdit = "UPDATE `$database_main`.`texts` SET `texts`.`text` = '$newtext' WHERE `texts`.`id` = '$sid' LIMIT 1" or die(mysql_error());
$retval = mysql_query( $doArtistEdit, $main );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
header("Location: ".$_SERVER['HTTP_REFERER']);
}
?>
This is in the form code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="$ID" method="post">
Song Name : <input type="text" name="title" value="<?php echo $row_song['title']; ?>"> by Artist Name :<input type="text" name="artist" value="<?php echo $row_artist['artist']; ?>"><br><?php echo $sid; ?><?php echo $database_main; ?><?php echo $doTextEdit; ?>
Lyrics :<br><textarea name="text" cols="72" rows="20"><?php echo str_replace("\n","\n",$row_Recordset1['text']); ?></textarea><br>
<input type="submit" name="editlyrics" value="Edit"></form>
You are checking if $_POST['submit'] is set -
if (isset($_POST['submit']))
But your submit button name is editlyrics, so it should be -
if (isset($_POST['editlyrics']))
Add a hidden input inside your form -
<input type="hidden" name="song_id" value="<?php echo $sid; ?>">
And now your code would be something like-
<?
if (isset($_POST['editlyrics'])) {
$song_id = mysql_real_escape_string($_POST['song_id']);
$newtext = mysql_real_escape_string($_POST['text']);
$doTextEdit = "UPDATE `$database_main`.`texts` SET `texts`.`text` = '$newtext' WHERE `texts`.`id` = '$song_id' LIMIT 1" or die(mysql_error());
$retval = mysql_query( $doTextEdit, $main );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
header("Location: ".$_SERVER['HTTP_REFERER']);
}
?>
Note that you should not be writing code using the outdated mysql_ functions, and learn either mysqli or PDO - php.net/manual/en/mysqlinfo.api.choosing.php
You are setting up a SQL query in $doTextEdit but you are passing $doArtistEdit to the mysql_query() function.

how to update postgresql rows using php?

first I want to say that I'm a beginner in postgresql and php.. my company told me to create a database that they can view and edit on local server.. so I created the database in postgresql.. created a page that views the database:
<html>
<head>
<title>Ongoing projects</title>
</head>
<body bgcolor="666657">
<?php
//database access information
require_once("DB.php");
$host = "localhost";
$user = "admin";
$pass = "";
$db = "Project_list";
$port = "5432";
//open a connection to the database server
$connection = pg_connect("host=$host dbname=$db user=$user password=$pass port=$port");
if (!$connection)
{
die("Could not open connection to database server");
}
?>
<?php
$query = 'select * from ongoing';
$result = pg_query($query); $i = 0;
echo '<html><table bgcolor="666657" width="10" height="30" border="0" cellpadding="0" cellspacing="0"><td align="center"> <h1><font color = "#ffb200"> Ongoing projects</h1>';
echo '<html><body><table border= 2 BORDERCOLOR="000000" cellpadding="1" cellspacing="0"> <tr >';
while ($i < pg_num_fields($result)) {
$fieldName =pg_field_name($result, $i);
echo '<b>'.'<td width="2" bgcolor="666657" align="center">'.'<font color = "#ffb200">'. '</b>'.'<b>'. $fieldName . '</b>'. '</td>';
$i = $i + 1; }
echo("<td><align= center><font color = #ffb200><b>Action</td>");
echo '</tr>' ;
$i = 0;
while ($row = pg_fetch_row($result)) {
echo '<tr align="center" width="1">';
$count = count($row);
$y = 0;
while ($y < $count) {
$c_row = current($row);
echo '<td>' .'<font color = "#ffb200">'. $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo("<td><align= center><a href='editongoing.php?ProjectID=".$row[0]."'>Edit</a></td>");
echo '</tr>';
$i = $i + 1;
}
pg_free_result($result);
echo '</table></body></html>';
?>
<h3>
<a href="projects.php"</a>Back to projects page</a>
</h3>
<SCRIPT LANGUAGE="JavaScript">
if (window.print) {
document.write('<form> '
+ '<input type=button name=print value="Click" '
+ 'onClick="javascript:window.print()"> To Print!</form>');
}
// End -->
</script>
when you click the edit button, you will go to this page where you can edit the raw you want, this is the (edit) code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Edit Ongoing projects</title>
</head>
<body bgcolor="666657">
<?php
// attempt a connection
$connection = pg_connect("host=localhost dbname=Project_list user=admin password=");
if (!$connection) {
die("Error in connection: " . pg_last_error());
}
if ($_REQUEST['ProjectID']!=''){
$QueryStr = "Select * from ongoing where project_no='".$_REQUEST['ProjectID']."'";
$result = pg_query($connection, $QueryStr);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
$row = pg_fetch_row($result);
print_r($row);
}
if ($_POST['submit']) {
// escape strings in input data
$project_no = pg_escape_string($_POST['project_no']);
$title = pg_escape_string($_POST['title']);
$duration = pg_escape_string($_POST['duration']);
$manager = pg_escape_string($_POST['manager']);
$country = pg_escape_string($_POST['country']);
$total_fee = pg_escape_string($_POST['totalfee']);
$performed = pg_escape_string($_POST['performed']);
$remaining = pg_escape_string($_POST['remaining']);
$gross_profit = pg_escape_string($_POST['gross_profit']);
$gp = pg_escape_string($_POST['gp']);
$performance_year = pg_escape_string($_POST['performance_year']);
$gp_year = pg_escape_string($_POST['gp_year']);
// execute query
$sql = "INSERT INTO ongoing (project_no, project_title, duration, manager, country, total_fee,
performed, remaining, gross_profit, gp, performance_year, gp_year)
VALUES('$project_no', '$title', '$duration', '$manager', '$country','$total_fee','$performed','$remaining',
'$gross_profit','$gp', '$performance_year','$gp_year')";
$result = pg_query($connection, $sql);
f (!$result) {
die("Error in SQL query: " . pg_last_error());
}
echo "Data successfully inserted!";
// free memory
pg_free_result($result);
// close connection
pg_close($connection);
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><b><font color = "#ffb200">
Project No.: <br> <input id="project_no" type="text" name="project_no" size="20" value=<?= $row[0] ?>>
<p>
Project Title: <br> <input id="title" type="text" name="title" value='<?= $row[1] ?>'>
<p>
Duration: <br> <input ID="duration" type="text" name="duration" value=<?= $row[2] ?>>
<p>
Project Manager: <br> <input ID="manager" type="text" name="manager" value=<?= $row[3] ?>>
<p>
Country: <br> <input ID="country" type="text" name="country" value=<?= $row[4] ?>>
<p>
Total Fee: <br> <input ID="totalfee" type="text" name="total_fee" value=<?= $row[5] ?>>
<p>
Already performed: <br> <input ID="performed" type="text" name="performed" value=<?= $row[6] ?>>
<p>
Remaining performance: <br> <input ID="remaining" type="text" name="remaining" value=<?= $row[7] ?>>
<p>
Gross Profit: <br> <input ID="gross_profit" type="text" name="gross_profit" value='<?= $row[8] ?>'>
<p>
GP%: <br> <input ID="gp" type="text" name="gp" value=<?= $row[9] ?>>
<p>
Performance actual year: <br> <input ID="performance_year" type="text" name="performance_year" value=<?= $row[10] ?>>
<p>
GP actual year: <br> <input ID="gp_year" type="text" name="gp_year" value=<?= $row[11] ?>>
<p>
<input type="submit" name="submit" value="Sumbit my table" size="30">
<P>
<a href="ongoing.php"</a>View ongoing projects</a>
<a href="editproject.php"</a>Back to editing menu</a>
</form>
</body>
</html>
My problem is, when I edit the data and click on submit my table, a new raw is inserted.. but I want it to be updated not inserted... help plz
You need to select which record (id number) you want to update, and then your query will look like
$sql = "UPDATE ongoing SET field1='value', field2='value' ... WHERE id = 'id of project you want to edit'";

Categories