/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Update Script Help

Hello:

I’m trying to write a script to update a table. I have two scripts. Script 1 has the user select a program and a lender. When they click Submit, we go to Script 2 which extracts the current information for the selected program and lender.

The objective is to allow the user update the information for the selected program and lender. I am basically losing my mind. I have tried a number of ways to get the update to work and I’m not having any luck. Everytime, I would type in the new information and click Submit, I get various messages, such as undefined index.

I was hoping someone can help me out. I’m not sure what else to do. Something tells me that I need to have a Script 3 which would do the actual update. I was hoping to have the update performed in Script 2.

The code for both scripts are at the end. The versions you see reflect the point to which the scripts are working. In this case, you will see the current information for the program and lender. The scripts do not contain any of the update attempts I’ve made. I made so many, I wouldn’t even know what to display.

I was hoping by looking at what works, someone can point me in the right direction on how to proceed with the update portion.

Thank you very much for all the help. Here are the scripts.
====

Script 1: Two select boxes for the user to select a program and lender.

[code=php]
<?php

$page_title = ‘Rate Insert and Update’;

session_start();
$status = $_SESSION[“status”];
switch ($status)
{
case “Not logged”:
include “login.php”;
break;
case “”:
include “login.php”;
break;
case “Logged”:

include (‘header4.html’);

require_once (‘mysql_connect.php’);

?>
<form action=”test1.php” method=”post”>
<table>

<tr><td>Loan Program</td>
<td><select name=”program_id”>
<option value=””>Please Select</option>
<?php
$sql = “SELECT * FROM loan_program”;
$resultset = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_assoc( $resultset ) ) {
$program_id = $row[‘id’];
$program = $row[‘program’];

print ‘<option value=”‘ . $program_id . “” >” . $program . “</option>n”;
}
?>
</select></td>
</tr>

<tr><td>Lender</td>
<td><select name=”lender_id”>
<option value=””>Please Select</option>
<?php
$sql = “SELECT * FROM lender”;
$resultset1 = mysql_query( $sql ) or die(mysql_error());
while( $row1 = mysql_fetch_assoc( $resultset1 ) ) {
$lender_id = $row1[‘id’];
$lender = $row1[‘lender’];
print ‘<option value=”‘ . $lender_id . “” >” . $lender . “</option>n”;
}
?>
</select></td>
</tr>
<tr><td></td></tr>

<tr><td><label for=”blank”>&nbsp;</label></td><td><input type=”submit” name=”btnSubmit” id=”btnSubmit” value=”Submit” class=”btn” />
<input type=”hidden” name=”submitted” value=”TRUE” /></td></tr>

</table>

</form>

<p><a href=”welcome.php”>Return to Main Menu</a></p>
<?php
include(‘footer3.html’);
?>
<?
}
?>
[/code]

====
Script 2: Passes the program and lender from script 1 and displays the current information for that program and lender.

[code=php]
<?php

$page_title = ‘Rate Insert and Update’;

session_start();
$status = $_SESSION[“status”];
switch ($status)
{
case “Not logged”:
include “login.php”;
break;
case “”:
include “login.php”;
break;
case “Logged”:

include (‘header4.html’);
require_once (‘mysql_connect.php’);

$pid = $_POST[‘program_id’];
$lid = $_POST[‘lender_id’];

$sql = “SELECT * FROM loan_program WHERE id=$pid”;
$resultset = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_assoc( $resultset ) ) {
$program_id = $row[‘id’];
$program = $row[‘program’];
}

$sql = “SELECT * FROM lender WHERE id=$lid”;
$resultset1 = mysql_query( $sql ) or die(mysql_error());
while( $row1 = mysql_fetch_assoc( $resultset1 ) ) {
$lender_id = $row1[‘id’];
$lender = $row1[‘lender’];
}

echo “Update Rate<br><br>$program<br>$lender”;
$sql = “SELECT * FROM test WHERE program_id=$pid AND lender_id=$lid”;
$resultset2 = mysql_query( $sql ) or die(mysql_error());
$row2 = mysql_fetch_assoc( $resultset2 );
$rate1 = $row2[‘rate1’];
$price1 = $row2[‘price1’];
$rate2 = $row2[‘rate2’];
$price2 = $row2[‘price2’];
$rate3 = $row2[‘rate3’];
$price3 = $row2[‘price3’];
$rate4 = $row2[‘rate4’];
$price4 = $row2[‘price4’];

?>

<form action=”test1.php” method=”post”>
<table>

<tr><td>Rate</td><td>Price</td>
<tr><td> <input type=”text” name=”rate1″ size=”7″ maxlength=”10″ value=”<?php echo $rate1; ?>” /></td>
<td><input type=”text” name=”price1″ size=”7″ maxlength=”10″ value=”<?php echo $price1; ?>” /></td></tr>
<tr><td> <input type=”text” name=”rate2″ size=”7″ maxlength=”10″ value=”<?php echo $rate2; ?>” /></td>
<td><input type=”text” name=”price2″ size=”7″ maxlength=”10″ value=”<?php echo $price2; ?>” /></td></tr>
<tr><td> <input type=”text” name=”rate3″ size=”7″ maxlength=”10″ value=”<?php echo $rate3; ?>” /></td>
<td><input type=”text” name=”price3″ size=”7″ maxlength=”10″ value=”<?php echo $price3; ?>” /></td></tr>
<tr><td> <input type=”text” name=”rate4″ size=”7″ maxlength=”10″ value=”<?php echo $rate4; ?>” /></td>
<td><input type=”text” name=”price4″ size=”7″ maxlength=”10″ value=”<?php echo $price4; ?>” /></td></tr>

<tr><td><label for=”blank”>&nbsp;</label></td><td><input type=”submit” name=”btnSubmit” id=”btnSubmit” value=”Submit” class=”btn” />
<input type=”hidden” name=”submitted” value=”TRUE” /></td></tr>

</table>

</form>
<p><a href=”welcome.php”>Return to Main Menu</a></p>

<?php
include(‘footer3.html’);
?>
<?
}
?>
[/code]

Now, all I need is for the user to type over the current information, click Submit, and update the table. This is where I am failing and could use some help/guidance.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Mar 06.2007 — First, a minor rewrite of Script2.php.

(1) Don't use SWITCH/CASE unless you absolutely must,

(2) Note the use of isset() to test the session variable, and

(3) Use LIMIT 1 in your SQL statement where you only expect 1 row.[code=php]<?php
$page_title = 'Rate Insert and Update';

session_start();
$status = isset($_SESSION['status']) ? $_SESSION['status'] : "";

IF ($status == "Not logged" OR $status == "") :
include "login.php";
ELSEIF ($status == "Logged") :
include ('header4.html');
require_once ('mysql_connect.php');

$pid = $_POST['program_id'];
$lid = $_POST['lender_id'];

$sql = "SELECT * FROM loan_program WHERE id=$pid LIMIT 1";
$resultset = mysql_query( $sql ) or die(mysql_error());

$row = mysql_fetch_array($resultset);
$program_id = $row['id'];
$program = $row['program'];

$sql = "SELECT * FROM lender WHERE id=$lid LIMIT 1";
$resultset1 = mysql_query($sql) OR die(mysql_error());
$row1 = mysql_fetch_array($resultset1);
$lender_id = $row1['id'];
$lender = $row1['lender'];

echo "Update Rate<br><br>$program<br>$lender";

$sql = "SELECT * FROM test WHERE program_id=$pid AND lender_id=$lid LIMIT 1";
$resultset2 = mysql_query( $sql ) or die(mysql_error());
$row2 = mysql_fetch_array($resultset2);
$rate1 = $row2['rate1'];
$price1 = $row2['price1'];
$rate2 = $row2['rate2'];
$price2 = $row2['price2'];
$rate3 = $row2['rate3'];
$price3 = $row2['price3'];
$rate4 = $row2['rate4'];
$price4 = $row2['price4'];
?>

<form action="test1.php" method="post">
<input type='hidden' name='program_id' value='<?php echo $pid ?>'>
<input type='hidden' name='lender_id' value='<?php echo $lid ?>'>
<input type="hidden" name="submitted" value="TRUE" />
<table>
<tr>
<td>Rate</td>
<td>Price</td>
</tr>
<tr>
<td><input type="text" name="rate1" size="7" maxlength="10" value="<?php echo $rate1; ?>" /></td>
<td><input type="text" name="price1" size="7" maxlength="10" value="<?php echo $price1; ?>" /></td>
</tr>
<tr>
<td><input type="text" name="rate2" size="7" maxlength="10" value="<?php echo $rate2; ?>" /></td>
<td><input type="text" name="price2" size="7" maxlength="10" value="<?php echo $price2; ?>" /></td>
</tr>
<tr>
<td><input type="text" name="rate3" size="7" maxlength="10" value="<?php echo $rate3; ?>" /></td>
<td><input type="text" name="price3" size="7" maxlength="10" value="<?php echo $price3; ?>" /></td>
</tr>
<tr>
<td><input type="text" name="rate4" size="7" maxlength="10" value="<?php echo $rate4; ?>" /></td>
<td><input type="text" name="price4" size="7" maxlength="10" value="<?php echo $price4; ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" name="btnUpdateIt" id="btnUpdateIt" value="Submit" class="btn" />
</td>
</tr>
</table>
</form>
<p><a href="welcome.php">Return to Main Menu</a></p>
<?php
include('footer3.html');
ENDIF;
?>[/code]
Now, a possible update script (test1.php):[code=php]<?php
session_start();
$status = isset($_SESSION['status']) ? $_SESSION['status'] : "";

IF ($status == "Not logged" OR $status == "") :
include "login.php";
ELSEIF ($status == "Logged") :
IF (!isset($_POST['btnUpdateIt'])) :
// Coming from wrong form/script
ELSE :
require_once ('mysql_connect.php');

$pid = $_POST['program_id'];
$lid = $_POST['lender_id'];

$sql = "INSERT INTO test
SET program_id = $pid,
lender_id = $lid,
rate1 = " . $_POST['rate1'] . ",
price1 = " . $_POST['price1'] . ",
rate2 = " . $_POST['rate2'] . ",
price2 = " . $_POST['price2'] . ",
rate3 = " . $_POST['rate3'] . ",
price3 = " . $_POST['price3'] . ",
rate4 = " . $_POST['rate4'] . ",
price4 = " . $_POST['price4'] . "
WHERE program_id = $pid
AND lender_id = $lid
ON DUPLICATE KEY
UPDATE rate1 = " . $_POST['rate1'] . ",
price1 = " . $_POST['price1'] . ",
rate2 = " . $_POST['rate2'] . ",
price2 = " . $_POST['price2'] . ",
rate3 = " . $_POST['rate3'] . ",
price3 = " . $_POST['price3'] . ",
rate4 = " . $_POST['rate4'] . ",
price4 = " . $_POST['price4'] ;

$resultset2 = mysql_query( $sql ) or die(mysql_error());
$row2 = mysql_fetch_array($resultset2);
ENDIF;
ENDIF;

// Update finished: Take user to next page...
header("Location: http://www.domain.com/nextpage.php");

?>[/code]
Copy linkTweet thisAlerts:
@focus310authorMar 06.2007 — Hello:

First, thank you very much for your reply. I wasn't expecting all this. I do appreciate it.

I modified my second script per your suggestions.

Script 1 works nicely. I'm able to make my selections and submit. After clicking submit, script 2 works nicely (after I used your re-writes) and the current information for my selections appear on the screen.

I created a third script which will do the update/insert.

When I attempt to make changes and then click the Submit button to do the update; I receive the following message:

Parse error: syntax error, unexpected T_STRING in C:InetpubfullfocusInformedtest2.php on line 50

I can't seem to find what is causing this message.

This is the script that is causing this message:
[code=php]
<?php

session_start();
$status = isset($_SESSION['status']) ? $_SESSION['status'] : "";

IF ($status == "Not logged" OR $status == "") :
include "login.php";
ELSEIF ($status == "Logged") :
IF (!isset($_POST['btnUpdateIt'])) :
// Coming from wrong form/script
ELSE :
require_once ('mysql_connect.php');

$pid = $_POST['program_id'];
$lid = $_POST['lender_id'];

$sql = "INSERT INTO test
SET program_id = $pid,
lender_id = $lid,
rate1 = " . $_POST['rate1'] . ",
price1 = " . $_POST['price1'] . ",
rate2 = " . $_POST['rate2'] . ",
price2 = " . $_POST['price2'] . ",
rate3 = " . $_POST['rate3'] . ",
price3 = " . $_POST['price3'] . ",
rate4 = " . $_POST['rate4'] . ",
price4 = " . $_POST['price4'] . ",
date = CURRENT_DATE,
time = CURRENT_TIME
WHERE program_id = $pid
AND lender_id = $lid
ON DUPLICATE KEY
UPDATE rate1 = " . $_POST['rate1'] . ",
price1 = " . $_POST['price1'] . ",
rate2 = " . $_POST['rate2'] . ",
price2 = " . $_POST['price2'] . ",
rate3 = " . $_POST['rate3'] . ",
price3 = " . $_POST['price3'] . ",
rate4 = " . $_POST['rate4'] . ",
price4 = " . $_POST['price4'] . ",
date = CURRENT_DATE,
time = CURRENT_TIME ;

$resultset2 = mysql_query( $sql ) or die(mysql_error());
$row2 = mysql_fetch_array($resultset2);
ENDIF;
ENDIF;

// Update finished: Take user to next page...
header("Location: welcome.php");

?>
[/code]


Thank you for the help.
Copy linkTweet thisAlerts:
@MrCoderMar 06.2007 — [code=php]
<?php

session_start();
$status = isset($_SESSION['status']) ? $_SESSION['status'] : "";

IF ($status == "Not logged" OR $status == "") :
include "login.php";
ELSEIF ($status == "Logged") :
IF (!isset($_POST['btnUpdateIt'])) :
// Coming from wrong form/script
ELSE :
require_once ('mysql_connect.php');

$pid = $_POST['program_id'];
$lid = $_POST['lender_id'];

$sql = "INSERT INTO test
SET program_id = $pid,
lender_id = $lid,
rate1 = " . $_POST['rate1'] . ",
price1 = " . $_POST['price1'] . ",
rate2 = " . $_POST['rate2'] . ",
price2 = " . $_POST['price2'] . ",
rate3 = " . $_POST['rate3'] . ",
price3 = " . $_POST['price3'] . ",
rate4 = " . $_POST['rate4'] . ",
price4 = " . $_POST['price4'] . ",
date = CURRENT_DATE,
time = CURRENT_TIME
WHERE program_id = $pid
AND lender_id = $lid
ON DUPLICATE KEY
UPDATE rate1 = " . $_POST['rate1'] . ",
price1 = " . $_POST['price1'] . ",
rate2 = " . $_POST['rate2'] . ",
price2 = " . $_POST['price2'] . ",
rate3 = " . $_POST['rate3'] . ",
price3 = " . $_POST['price3'] . ",
rate4 = " . $_POST['rate4'] . ",
price4 = " . $_POST['price4'] . ",
date = CURRENT_DATE,
time = CURRENT_TIME";

$resultset2 = mysql_query( $sql ) or die(mysql_error());
$row2 = mysql_fetch_array($resultset2);
ENDIF;
ENDIF;

// Update finished: Take user to next page...
header("Location: welcome.php");

?>
[/code]
Copy linkTweet thisAlerts:
@focus310authorMar 06.2007 — Hi,

I made the change. When I run the script, I now come up with this message:

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 'WHERE program_id = 1 AND lender_id = 1 ON DUPLICAT' at line 14

I've looked over some manuals, I don't seem to see the problem. Are you able to spot something?
Copy linkTweet thisAlerts:
@NightShift58Mar 06.2007 — Remove these two lines form the SQL statement. I started out as an update, then turned it into an insert and these are leftovers no one needs...especially not in an insert statement... Sorry...[code=php]WHERE program_id = $pid
AND lender_id = $lid[/code]
Copy linkTweet thisAlerts:
@focus310authorMar 06.2007 — Hi,

That worked!

Thank you for all the help.
×

Success!

Help @focus310 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...