Page 1 of 1

Unable to update database through PHP

Posted: Sun Jan 26, 2014 10:13 pm
by satish_apc
***** Please use "PHP Code" tag *****

first File:

Code: Select all

<html>
	<head>
		<title>Maildata record browser</title>
	</head>
	<body bgcolor="#FFFFFF" text="#000000">
	<?php
	require_once 'login.php';
	$db_server = MySQL_connect($db_hostname, $db_username, $db_password);
	mysql_select_db('Mailinglist')or die("Unable to select database: " . mysql_error());
	$sqlquery = "SELECT * from Maildata";
	$queryresult = mysql_query($sqlquery);
	echo "<table width=700 border=1 align=center>";
		echo " <tr>";
			echo "<td width=200> <center><b>Name</b></center></td>\n";
			echo "<td width=200> <center><b>e-mail</b></center></td>\n";
			echo "<td width=200> <center><b>Secondary email</b></center></td>\n";
			echo " <td width=100> <center><b>Action</b></center></td>\n";
		echo " </tr>\n";
		while ($row=mysql_fetch_array($queryresult)){
		echo " <tr>\n";
			echo " <td>".$row["Name"]."</td>\n";
			echo " <td>".$row["Email"]."</td>\n";
			echo " <td>".$row["Secondaryemail"]."</td>\n";
			echo " <td><a href=\"display.php?Email=".$row["Email"]."\">edit</a></td>\n";
		echo " </tr>\n";
		}
		echo "</table>\n";
	?>
	</body>
</html>
OUTPUT: Here through edit i will be taken to another file display.php

Code: Select all

<html>
	<head>
		<title>Mail record update form</title>
	</head>
	<body bgcolor="#FFFFFF" text="#000000">
	<form name="maildata" method="post" action="update.php">
	<table width="250" border="1" align="center">
	<?php
	require_once 'login.php';
	$db_server = MySQL_connect($db_hostname, $db_username, $db_password);
	$selectdb=mysql_select_db("Mailinglist") or die("Could not select mail data database !");
	extract($_REQUEST, EXTR_SKIP);
	$sqlquery = "SELECT * from Maildata";
	$queryresult = mysql_query($sqlquery);
	if($row=mysql_fetch_array($queryresult)){
	echo "<tr> ";
		echo " <td> Name </td>";
		echo " <td width=\"150\">". $row["Name"] . " </td>";
	echo "</tr>";
	echo "<tr> ";
		echo " <td> e-mail </td>";
		echo " <td>". $row["Email"]."</td>";
		echo " <td> <input type=\"hidden\" Name=\"Email\"value=\"".$row["Secondaryemail"]."\" ></td>";
	echo "</tr>";
	echo " <tr> ";
		echo " <td> secondarye-mail </td>";
		echo " <td> ";
		echo " <input type=\"text\" Name=\"Secondaryemail\" value=\" ".$row["Secondaryemail"]."\">";
		echo " </td>";
	echo "</tr>";
	echo "<tr> ";
		echo " <td> ";
	echo " <center> ";
	echo " <input type=\"submit\"name=\"Submit\" value=\"Submit\">";
	echo " </center>";
		echo " </td>";
	echo "</tr>";
	}
	?>
	</table>
	</form>
	</body>
</html>
OUTPUT: here i will do some changes to last field value from satish_apc@yahoo.com to satish_apc and click on submit. It will take me to update.php

Code: Select all

<html>
	<head>
		<title>New record In Newsmail table</title>
		<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
	</head>
	<body bgcolor="#FFFFFF" text="#000000">
	<p>&nbsp;</p>
	<?php
	require_once 'login.php';
	$db_server = MySQL_connect($db_hostname, $db_username, $db_password);
	$selectdb=mysql_select_db("Mailinglist") or die("Could not select mail data database !");
	extract($_REQUEST, EXTR_SKIP);
	$sqlquery = "UPDATE Maildata SET Secondaryemail=\"".$secondaryemail."\" where Email=\"".
$Email."\"";
	echo $sqlquery;
	
	$result = mysql_query($sqlquery)or die("Could not execute SQL query");
	if(!$result){ 
		die("Unable to insert data into table maildata:".'<br />'.mysql_error());
	} 
	echo " Record was successfully updated.";
?>
</body>
</html>
OUTPUT: UPDATE Maildata SET Secondaryemail="" where Email="satish_apc@yahoo.com" Record was successfully updated.

But database wont get updated. Help me out.

Re: Unable to update database through PHP

Posted: Mon Jan 27, 2014 2:34 pm
by social_experiment
satish_apc wrote:OUTPUT: UPDATE Maildata SET Secondaryemail="" where Email="satish_apc@yahoo.com" Record was successfully updated.
In this line you will notice the value that you want to write to the database ($secondaryemail) doesn't contain anything. Where does $secondaryemail come from? Is it from the form? Where is it defined?

Also look at your code

Code: Select all

<?php
$result = mysql_query($sqlquery)or die("Could not execute SQL query");
        if(!$result){ 
                die("Unable to insert data into table maildata:".'<br />'.mysql_error());
        } 
        echo " Record was successfully updated.";
?>
you last echo occurs, regardless of what happens to your query; whether it fails, succeeds. What you want is something to the effect of

Code: Select all

<?php
 if (!$result) {
    // query failed, issue message to user.
 }
 else {
   // query works, issue message to user.
 }
?>