0

Hello. I have displayed data from SQL Table in the HTML form and I want to use this same form to update COMMENT field in the sql. I have already coded my page as follows;

<?php

// Connect to server and select databse.
$con=mysql_connect("localhost", "root", "")or die("cannot connect");
mysql_select_db("ison",$con)or die("cannot select DB");

if(isset($_POST["button"]))



    {


$sql="UPDATE customer SET
COMMENT='$_POST[COMMENT]' WHERE MSISDN='$MSISDN'";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  else
  {
      echo '<span style="color:#6f892c;">RECORD INSERTED SUCCESSFULLY...</span>';
      header("location:index.php");
  }
}

$result = mysql_query("SELECT * FROM customer");

?>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Pending Report</title>
</head>
<body>
<form  method="post" action="">

<table width="1200" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1200" border="1" cellspacing="1" cellpadding="3">
<tr>
</tr>

<tr>
<td align="center"><strong>S.NO</strong></td>
<td align="center"><strong>MSISDN</strong></td>
<td align="center"><strong>CUSTOMERNAME</strong></td>
<td align="center"><strong>TIME</strong></td>
<td align="center"><strong>COMPANY</strong></td>
<td align="center"><strong>REASON</strong></td>
<td align="center"><strong>CLOSED</strong></td>
<td align="center"><strong>COMMENT</strong></td>

</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['S_NO']; ?></td>
<td><?php echo $rows['MSISDN']; ?></td>
<td><?php echo $rows['CUSTOMERNAME']; ?></td>
<td><?php echo $rows['TIME']; ?></td> 
<td><?php echo $rows['COMPANY']; ?></td>
<td><?php echo $rows['REASON']; ?></td>
<td><input name="CLOSED" type="checkbox" id="CLOSED"  value="<?php echo $rows['CLOSED']; ?>"  size="15" checked /> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="<?php echo $rows['COMMENT']; ?>" size="15"/>
</td>


<?php
}

?>


</table>
</td>
</tr>
</table>
<input type="submit" name="button" id="button" value="Submit" /></td></tr>
</form>
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "20";
?>
<html>
    <head>
    <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
    </head>
    <body>
    <?php
        echo "Page is refreshing...!";
    ?>

</body></html>

The problem is that even after submit button, there is no update, and am getting SUCCESSFULLY UPDATED results.
Can someone assist please.

3
Contributors
34
Replies
107
Views
2 Years
Discussion Span
Last Post by Makara
0

Print out your sql statement and if it looks ok would be a good place to start.

I don't do PHP but the Where statement looks suspicious to me.

0

Looks like $MSISDN is not given a value, so the query works but does not find any matching rows.

Next to that, directly injecting a comment into your query is likely to break it, for example when using apostrophes. You really need to do some sanitizing.

0

Dave I have included an echo to $sql but nothing happens. Pritaeas kindly expound your ans by giving an example please.

0

Something like this I think?

$sql="UPDATE customer SET COMMENT='$_POST[COMMENT]' WHERE MSISDN= '$_POST[MSISDN]';"

0

Dave av used this sql but am getting Undefined index: MSISDN in line 8

0

The comment field is the oner that I want to update

0

Ok thought so.

So when you post back, you need to update all comments right? It looks like you are just running one sql statement at the moment right?

0

Sure. But the field that I nid it be updated is the COMMENT field. But if we can update them all at the same time it will be fine. I need the form to do the update

0

Ok its a bit more complicated that it first appeared then.

I'm off out now,might be able to help later. Meanwhile maybe someone else cn help?

0

Sure. Anyone with this idea can assist please. I need to launch this application by the end of the day

0

I'm back now. Can you show me the rendered html - the view source - this will make it easy for me to explain what you need to do.

0

Ok this is the code that is inserting the data into the sql table before getting the sql table displayed in the html form.

<?php
error_reporting(0);
$connection = mysql_connect("localhost", "root", ""); // Establishing connection with server..
$db = mysql_select_db("ison", $connection); // Selecting Database.
//Array to store validation errors
    if ($_SERVER['REQUEST_METHOD']=='POST') {
    $errmsg_arr = array();

    //Validation error flag
    $errflag = false;

        //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
    foreach ($_POST as $key => $val) {
      $form[$key] = htmlspecialchars($val);
    }
        //Sanitize the POST values

    $MSISDN = clean($_POST['MSISDN']);
    $CUSTOMERNAME = clean($_POST['CUSTOMERNAME']);

    //Input Validations

    if($MSISDN == '') {
        $errmsg_arr[] = 'MSISDN missing';
        $errflag = true;
    }
    if($CUSTOMERNAME == '') {
        $errmsg_arr[] = 'CUSTOMERNAME missing';
        $errflag = true;
    }
    }
if(isset($_POST["button"]))
{
$MSISDN=$_POST['MSISDN'];
$query = "SELECT * FROM `customer` WHERE `MSISDN` = '{$MSISDN}'";
$result = mysql_query($query);
$sql="INSERT INTO customer (MSISDN, CUSTOMERNAME, TIME, COMPANY, REASON,CLOSED,COMMENT)
VALUES('$_POST[MSISDN]','$_POST[CUSTOMERNAME]','$_POST[TIME]','$_POST[COMPANY]','$_POST[REASON]','$_POST[CLOSED]','$_POST[COMMENT]')";
header('Location: index.php');
      echo '<span style="color:#6f892c;">RECORD INSERTED SUCCESSFULLY...</span>';
if (!mysql_query($sql,$connection))
  {
  die('Error: ' . mysql_error());
  }
  else
  {    header('Location: index.php');
      echo '<span style="color:#6f892c;">RECORD INSERTED SUCCESSFULLY....</span>';

  }
}

$result = mysql_query("SELECT * FROM customer");

?> 

<!DOCTYPE html>
<html><head>
<title>Customer Call Details</title>
</head>
<body><BODY  bgcolor="#00CED1">
<div id="main">
<div id="login">
    <div align="center"><font size="+2">AGENT</font></div><hr>

    <div align="center">
      <script>window.jQuery || document.write('<script src="js/jquery-1.6.1.min.js"><\/script>')</script>

       <style type="text/css">
.InputClass {
  font-size: 46px;
}
</style>
      </head>

      <body id="top">

      <BODY  bgcolor="#00CED1">

      <!-- header
============================================================================= -->
      <!-- featured
============================================================================== -->
    </div>
  <div id="featured-wrap" align="center" >

    <div align="center">
      <article id="featured" class="clearfix">
      <script type="text/javascript">
function ValidateEmail(uemail)  
{  
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
if(uemail.value.match(mailformat))  
{  
return true;  
}  
else  
{  
alert("You have entered an invalid email address!");  
uemail.focus(); 
uemail.value = "" 
return false;  
}  
}  
function clearText(firstname){
if (firstname.defaultValue==firstname.value)
firstname.value = ""
}
function allnumeric(idno)  
{   
var numbers = /^[0-9]+$/;  
if(idno.value.match(numbers))  
{  
return true;  
}  
else  
{  
alert('use numeric characters only in this field ');  
idno.focus(); 
idno.value = "" 

return false;  
}  
}  
function fnamevalidation(firstname)  
{   
var letters = /^[A-Za-z]+$/;  
if(firstname.value.match(letters))  
{  
return true;  
}  
else  
{  
alert('firstname must have alphabet characters only');  
firstname.focus();
firstname.value = "" 
return false;  
}  
}  
function CUSTOMERNAMEvalidation(lastname)  
{   
var letters = /^[A-Za-z_]+$/;  
if(lastname.value.match(letters))  
{  
return true;  
}  
else  
{  
alert('CUSTOMER NAME must have alphabet characters only');  
lastname.focus();  
lastname.value = ""
return false;  
}  
}
function postaladdressvalidation(postaladdress)  
{   
var letters = /^[0-9a-zA-Z]+$/;  
if(postaladdress.value.match(letters))  
{  
return true;  
}  
else  
{  
alert('postal address must have alphanumeric characters only');  
postaladdress.focus(); 
postaladdress.value = "" 
return false;  
}  
} 

function emptyvalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}
        </script>
      </div>
      <h4 align="center" style="color:#6f892c;"><font size="+2">CALLER DETAILS </font></h4>
               <form name="form1" onSubmit="return validateForm(this);" method="post" action="" >

                 <div align="center">
                   <table align="center" cellpadding="2" cellspacing="0" border="1">
  <td><table width="994" border="1">
    <tr>
      <td valign="top"><font size="+2">MSISDN:</font></td>
      <td valign="top"><label for="label4"><font size="+2">CUSTOMER NAME:</font></label></td>
      <td valign="top"><font size="+2">TIME:</font></td>
      <td valign="top"><label for="label4"><font size="+2">COMPANY:</font></label></td>
    </tr>
    <tr>
      <td valign="top"><input type="text" name="MSISDN"   required="required" onChange="allnumeric(this,'Invalid input');"/></td>
      <td valign="top"><input type="text" name="CUSTOMERNAME"  required="required" onChange="CUSTOMERNAMEvalidation(this,'Invalid input');"/></td>
      <td valign="top"><input size="20%" type="text" name="TIME"  required="required" onChange="TIMEvalidation(this,'Invalid input');"/></td>
      <td valign="top"><input type="text" name="COMPANY" id="COMPANY" required="required"/></td>
    </tr>
  </table>

    <table width="994" border="1">
      <tr>
        <td width="100" valign="middle"><font size="+2">REASON:</font></td>
        <td width="900" valign="top"><input type="text" name="REASON" id="REASON" style="width: 870px;height:100px;"  /></td>
      </tr>
    </table>
    </td>
                   </table>
                 </div>
                 <br><br><br><br><br><br><br><br><br><br>
    <input  type="submit" name="button" id="button" value="Submit" class="InputClass"  style="width: 200px;height:80px">
    </form>
</div>
</div>

</body>
</html>
<br><marquee id="curved_border" style="font-family:Book Antiqua; color: #FFFFFF" bgcolor="#000080" scrolldelay="90" loop="infinite" behavior="" >
                            <img src="images/isonbpo-logo.png" width="71" height="39" alt=""/> Copyright ©ison bpo kenya ltd® 2015 <img src="images/isonbpo-logo.png" width="71" height="35" alt=""/> Designed and managed by Joseph Makara Tel_0788622626 <img src="images/isonbpo-logo.png" width="71" height="39" alt=""/>
    </marquee>
0

Sorry you have misunderstood what I asked for.

If you run your code so you are viewing the other page in your browser and then you view the source - ie the rendered html - then show me that please.

Thanks

0

When I run the code, it fetches the data from the sql table as shown in the screen shot below and displays it in html form.
wwwp.png

0

So my rendered page starts from

$result = mysql_query("SELECT * FROM customer");
<form method="post" action="">
<table width="1200" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1200" border="1" cellspacing="1" cellpadding="3">
<tr>
</tr>
<tr>
<td align="center"><strong>S.NO</strong></td>
<td align="center"><strong>MSISDN</strong></td>
<td align="center"><strong>CUSTOMERNAME</strong></td>
<td align="center"><strong>TIME</strong></td>
<td align="center"><strong>COMPANY</strong></td>
<td align="center"><strong>REASON</strong></td>
<td align="center"><strong>CLOSED</strong></td>
<td align="center"><strong>COMMENT</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['S_NO']; ?></td>
<td><?php echo $rows['MSISDN']; ?></td>
<td><?php echo $rows['CUSTOMERNAME']; ?></td>
<td><?php echo $rows['TIME']; ?></td>
<td><?php echo $rows['COMPANY']; ?></td>
<td><?php echo $rows['REASON']; ?></td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked /> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="<?php echo $rows['COMMENT']; ?>" size="15"/>
</td>
<?php
}
?>
</table>
</td>
</tr>
</table>
<input type="submit" name="button" id="button" value="Submit" /></td></tr>
</form>
0

Kindly expound this for me Dave. I think av not understood. What is happening is tht my table is directly fetched from the sql table. it has only that single page to extract that information to display it in an HTML form. Now the problem is that this form needs to update thesame table back to the server.

0

I got it Dave I think this will give you a greenlight

<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Pending Report</title>
</head>
<body>
<form method="post" action="">
<table width="1200" border="1" cellspacing="1" cellpadding="0">
<tbody><tr>
<td>
<table width="1200" border="1" cellspacing="1" cellpadding="3">
<tbody><tr>
</tr>
<tr>
<td align="center"><strong>S.NO</strong></td>
<td align="center"><strong>MSISDN</strong></td>
<td align="center"><strong>CUSTOMERNAME</strong></td>
<td align="center"><strong>TIME</strong></td>
<td align="center"><strong>COMPANY</strong></td>
<td align="center"><strong>REASON</strong></td>
<td align="center"><strong>CLOSED</strong></td>
<td align="center"><strong>COMMENT</strong></td>
</tr>
<tr>
<td>1</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>2</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>7</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>8</td>
<td>dfg</td>
<td>fdsgdsfg</td>
<td>dfgdsfg</td>
<td>ISON</td>
<td></td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>9</td>
<td>fcgdfg</td>
<td>fdgdsfg</td>
<td>dfgdsfg</td>
<td>dsfgsdfgdfg</td>
<td>fdsggggggggggggggggggffffffffffffffffffffffff</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>10</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>12</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>14</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>15</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>16</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>17</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>18</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>19</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>20</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>21</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>22</td>
<td>0788622626</td>
<td>eeyee</td>
<td>eyee</td>
<td>etetee</td>
<td>ttttt</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr><tr>
<td>34</td>
<td>0788622626</td>
<td>Yegon</td>
<td>444</td>
<td>4wdwrfw</td>
<td>gft4efef</td>
<td><input name="CLOSED" type="checkbox" id="CLOSED" value="" size="15" checked=""> Closed</td>
<td><input name="COMMENT" type="text" id="COMMENT" value="" size="15">
</td>
</tr></tbody></table>
</td>
</tr>
</tbody></table>
<input type="submit" name="button" id="button" value="Submit">
</form>


<meta http-equiv="refresh" content="20;URL='/supervisor/index.php'">


Page is refreshing...!</body></html>
0

Ok so the problem is when you post this form back you get a load of posted values all with the same name "Comment" - see my screenshot below.

So how do you know which record to update? What you need to do is give your form fields a name which identifies the row - eg Comment_1, Comment_2 etc.

Then you can parse the posted data and for each Comment update the correct record by parsing out the row id.

Does that help?

Fiddler.png

Edited by DaveAmour: typo

0

Its not working Dave. Can you send me a code for that case please

0

Sorry no I don't have a php development environment setup. Is this for your job or are you a student?

0

For a job. am working as web applications developer in a company in Kenya

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.