Jump to content

[SOLVED] Updating values, not working


ashrafzia

Recommended Posts

I am updating values, manually(using mysql from cmd)  its working fine, but on the web its not working.

I have a table in which student_id is primary key, and i want to update its value.

 

Heres the code:

 

$sql = " UPDATE $tbname SET

student_id = '$_POST[std_id]'

WHERE student_id = '$_POST[std_id]'   ";

 

value is echoing, i have checked it, but not working in the query.

Any idea ???

Link to comment
https://forums.phpfreaks.com/topic/73917-solved-updating-values-not-working/
Share on other sites

could you please post the rest of the code:

 

and you should first put the $_POST's into common variables, and the concoct them in the $sql variable, like this:

<?php
// common variables
$student_id = $_POST[std_id];

$sql = "UPDATE $tbname SET
    student_id = " . $student_id . "
    WHERE student_id = " . $student_id . "";

 

Regards ACE

Here's the full code:

<?php
include "connection.php";
include "menu.php";
$tbname = "student_info";

if (!isset($_GET['action'])){
$sql = "SELECT * from $tbname";
$result = mysql_query($sql, $conn) or die (mysql_error());
echo  "<br><br><br><br>
	<table border='1' align='center'>
	<tr class=yellow>
		<th>ID No</th>
		<th>Registration No</th>
		<th>Name</th>
		<th>Father Name</th>
		<th>Discipline</th>
		<th>Picture</th>
		<th>Command</th>
		<th>Command</th>

	</tr>";

while ($row = mysql_fetch_array($result)){
	$id = $row['student_id'];
	$reg = $row['registration_no'];
	$name = $row['student_name'];
	$fname = $row['father_name'];
	$prog = $row['programe'];
	$pic = $row['picture'];

	echo "<tr class=simple>
		<td align='center'>$id</td>
		<td align='center'>$reg</td>
		<td>$name</td>
		<td>$fname</td>
		<td>$prog</td>
		<td>$pic</td>
		<td align='center' class=tabval><a href=allstudents.php?action=edit&id=$id>[Edit]</a></td>
		<td align='center' class=tabval><a href=allstudents.php?action=del&id=$id>[Delete]</a></td>
		</tr>";
	}			
	echo "</table>";
}

if (isset($_GET['action'])){
$action = $_GET['action'];
$id = $_GET['id'];

if ($action=="del"){
$sql = "DELETE FROM $tbname WHERE student_id=$id";
$result = @mysql_query($sql, $conn) or die (mysql_error());
echo "Record Deleted Successfully....";

}
}

if (isset($_GET['action'])){
$action = $_GET['action'];
$id = $_GET['id'];

if ($action=="edit"){
	$sql = "SELECT * From $tbname WHERE student_id=$id";
	$result = @mysql_query($sql, $conn) or die (mysql_error());

	while ($row = mysql_fetch_array($result)){
	$id = $row['student_id'];
	$reg = $row['registration_no'];
	$name = $row['student_name'];
	$fname = $row['father_name'];
	$prog = $row['programe'];
	$pic = $row['picture'];

	$form = "<body>
			<p> </p>
			<p> </p>
			<form action='allstudents.php' method='post' enctype='multipart/form-data'>
		  	<table width='394' border='0' align='center' cellpadding='5' cellspacing='5'>
		    <tr>
		      <td width='135'>Studnet ID:</td>
		      <td width='218'><input name='std_id' type='text' size='5' value=$id></td>
		    </tr>
		    <tr>
		      <td>Registration No: </td>
		      <td><input name='registration_no' type='text' size='5' value=$reg></td>
    			</tr>
		    <tr>
		      <td>Student Name: </td>
		      <td><input name='student_name' type='text' value=$name></td>
		    </tr>
		    <tr>
		      <td>Father Name:</td>
		      <td><input name='father_name' type='text' value=$fname></td>
		    </tr>
		    <tr>
		      <td>Programe:</td>
		      <td><select name='programe'>
		        <option value='BBA'>BBA</option>
		        <option value='BBA-IT'>BBA-IT</option>
		        <option value='BCS'>BCS</option>
			      </select></td>
		    </tr>
		    <tr>
		      <td>Picture:</td>
		      <td><input name='picture' type='file' name='picture'></td>
		    </tr>
		    <tr>
		      <td colspan='2'><div align='center'><strong>
		        <input type='image' name='update' src='images/update.gif'>
		      </strong></div></td>
		    </tr>
		  </table>
		</form>
		</body>";

		echo "$form";
	}
}
}

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

/*
$sql = " UPDATE $tbname SET 
	 student_id = '$_POST[std_id]',
	 registration_no = '$_POST[registration_no]',
	 student_name = '$_POST[student_name]',
	 father_name = '$_POST[father_name]',
	 programe = '$_POST[programe]'
	 WHERE student_id = '$_POST[std_id]'

	  ";
*/
$id = "$_POST[std_id]";
echo "$id";

$sql = " UPDATE $tbname SET 
	 student_id = $id
	 WHERE student_id = '$_POST[std_id]'

	  ";


	  
$result = mysql_query ($sql, $conn) or die (mysql_error());
echo "Record Updated Successufully....";
echo "$_POST[std_id]";

}


?>

 

Problem is all other fields are updating but only the Student_id isn't updating......

I have checked and echoed the value, its printing but not working in the query...

I have a table in which student_id is primary key, and i want to update its value.

 

You can't update primary keys. It's a characteristic if a primary key: unique, not null, not updatable.

 

I have done it.

 

Actually i was having a logical flaw.

 

What actually i was trying to do is this :

I have the following table, with some records:

 

student_id | reg_id | std_name | father_name | picture etc......

1 reg_1 abc zxc

2 reg_2 abc zxc

3 reg_3 abc zxc

40 reg_40 abc zxc

5 reg_5 abc zxc

 

Now suppose if an admin wants to change the student_id of that student which has 40 as the student_id.

He will write certain query in mysql:

UPDATE student_info SET student_id = 4 WHERE student_id = 40;

It works fine in mysql.

 

I was trying to do the same thing but on web.

My mistake was: I was not getting the current value of the student_id to put inside the WHERE clause.

I simply stored the current value of student_id in a hidden form value by $_GET method.

When the form is submitted the New Value of student_id goes to the query and the previous actual value goes inside the Where Clause.

 

Its working Cooool!

Here's the Code:


if (isset($_GET['action'])){
        $action = $_GET['action'];
        $id_value = $_GET['id'];

        if ($action=="edit"){
       
                //echo "$id_value";
       
                $sql = "SELECT * From $tbname WHERE student_id=$id";
                $result = @mysql_query($sql, $conn) or die (mysql_error());
               
                while ($row = mysql_fetch_array($result)){
                $id = $row['student_id'];
                $reg = $row['registration_no'];
                $name = $row['student_name'];
                $fname = $row['father_name'];
                $prog = $row['programe'];
                $sem = $row['semester'];
                $pic = $row['picture'];
                               
                $form = "<body>
                                <p> </p>
                                <p> </p>
                                <form action='allstudents.php' method='post' enctype='multipart/form-data'>
                                <input type='hidden' value=$id_value name='h1'>
                                <table width='394' border='0' align='center' cellpadding='5' cellspacing='5'>
                            <tr>
                              <td width='135'>Studnet ID:</td>
                              <td width='218'><input name='std_id' type='text' size='5' value=$id></td>
                            </tr>
                            <tr>
                              <td>Registration No: </td>
                              <td><input name='registration_no' type='text' size='5' value=$reg></td>
                </tr>
                            <tr>
                              <td>Student Name: </td>
                              <td><input name='student_name' type='text' value=$name></td>
                            </tr>
                            <tr>
                              <td>Father Name:</td>
                              <td><input name='father_name' type='text' value=$fname></td>
                            </tr>
                            <tr>
                              <td>Programe:</td>
                              <td><select name='programe'>
                                <option value='BBA'>BBA</option>
                                <option value='BBA-IT'>BBA-IT</option>
                                <option value='BCS'>BCS</option>
                                      </select></td>
                            </tr>
                                <tr>
                                  <td>Semester:</td>
                                  <td><select name='semester'>
                                <option value='1st'>1st</option>
                                <option value='2nd'>2nd</option>
                                <option value='3rd'>3rd</option>
                                        <option value='4th'>4th</option>
                                <option value='5th'>5th</option>
                                <option value='6th'>6th</option>
                                        <option value='7th'>7th</option>
                                <option value='8th'>8th</option>
                              </select></td>
                          </tr>
                            <tr>
                              <td>Picture:</td>
                              <td><input name='picture' type='file' name='picture'></td>
                            </tr>
                            <tr>
                              <td colspan='2'><div align='center'><strong>
                                <input type='image' name='update' src='images/update.gif'>
                              </strong></div></td>
                            </tr>
                          </table>
                        </form>
                        </body>";
                                       
                        echo "$form";
                }
        }
}

if (isset($_POST['update_x'])){
$id_value = $_POST['h1'];

$sql = " UPDATE $tbname SET
                 student_id = '$_POST[std_id]',
                 registration_no = '$_POST[registration_no]',
                 student_name = '$_POST[student_name]',
                 father_name = '$_POST[father_name]',
                 programe = '$_POST[programe]',
                 semester = '$_POST[semester]'
                 WHERE student_id = $id_value
                               
                  ";
          
$result = mysql_query ($sql, $conn) or die (mysql_error());
echo "Record Updated Successufully....";
}
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.