14 Replies - 310 Views - Last Post: 15 January 2013 - 10:29 PM Rate Topic: -----

#1 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Problem in update

Posted 15 January 2013 - 08:38 PM

Hi., I want to update an entry that ive selected from a table.
Everything is running but the data does not update>

This is the code of the Table where I select the data.

<?php 

$conn=mysql_connect("localhost","root","");
	if(!$conn)
	{
	die('could not connect!' . mysql_error());
	}

	mysql_select_db("people", $conn);
$result = mysql_query("SELECT * FROM usersinfo"); ?>

<table>
  <tr>
    <td>Recent Posts</td>
  </tr>
  <?php while($row = mysql_fetch_array($result)) : ?>
  <tr>
    <td><?php echo $row['USERNAME']; ?></td>
    <td><?php echo $row['LASTNAME']; ?></td>
	    <td><?php echo $row['FIRSTNAME']; ?></td>
    <td><?php echo $row['MI']; ?></td>
	    <td><?php echo $row['COMPANY']; ?></td>
    <td><?php echo $row['POSITION']; ?></td>
	  <td><?php echo $row['DEPARTMENT']; ?></td>
    <!-- and so on -->
    <td>
      <form action="delete.php" method="post">
        <input type="hidden" name="delete_id" value="<?php echo $row['ID']; ?>" />
        <input type="submit" value="Delete" />
      </form>
	  <form action="modifypage.php" method="post">
		<input type="hidden" name="modify_id" value="<?php echo $row['ID']; ?>" />
        <input type="submit" value="Modify" />
      </form>
    </td>
  </tr>
  <?php endwhile; ?>
</table>


This is the code for the page where I input the new data

<?php
 echo"<form name='myForm' action='modify.php' method='post'>";
 echo"<br>";
 echo "Lastname: <input type='text' name='ace_firstname'>";
  echo"<br>";
 echo "Firstname: <input type='text' name='ace_lastname'>";
  echo"<br>";
 echo "Midlename: <input type='text' name='ace_middlename'>";
  echo"<br>";
 echo "Company: <input type='text' name='ace_project'>";
  echo"<br>";
 echo "Position: <input type='text' name='ace_department'>";
  echo"<br>";
 echo "Department: <input type='text' name='ace_position'>";
  echo"<br>";
   echo "id: <input type='hidden' name='ace_id' value='" . $modify . "'>";
  echo"<br>";
 echo "<input type='submit' value='Submit'>";
 
 

?>



and this is where the update happens

<?php

$MODfirstname=$_POST["ace_firstname"];
$MODlastname=$_POST["ace_lastname"];
$MODmiddlename=$_POST["ace_middlename"];
$MODproject=$_POST["ace_project"];
$MODdepartment=$_POST["ace_department"];
$MODposition=$_POST["ace_position"];
$MODid = ($_POST['ace_id']);

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("people", $con);

mysql_query("UPDATE usersinfo SET LASTNAME='" . $MODlastname . "', FIRSTNAME='" . $MODfirstname . "', MIDDLENAME='" . $MODmiddlename . "', PROJECT='" . $MODproject . "', DEPARTMENT='" . $MODdepartment . "', POSITION='" . $MODposition . "' WHERE ID=' " . $MODid . " '");

mysql_close($con);
?>



Can anyone review my code especially on the update query Ive probably missed out something . Thanks in advance. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Problem in update

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Problem in update

Posted 15 January 2013 - 08:44 PM

You're forgetting to test the result of your query like I suggested in your last thread! If there is a problem with your query, that will give us information to help you debug.

mysql_query(/*your query*/) or die("MySQL Error: " . mysql_error());


Was This Post Helpful? 1
  • +
  • -

#3 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem in update

Posted 15 January 2013 - 08:59 PM

View Postmacosxnerd101, on 15 January 2013 - 08:44 PM, said:

You're forgetting to test the result of your query like I suggested in your last thread! If there is a problem with your query, that will give us information to help you debug.

mysql_query(/*your query*/) or die("MySQL Error: " . mysql_error());



I added this code

if (mysql_query($sql , $con))
	{
	echo"saved!";
	}
else
	{
		echo"save failed!";
	}
mysql_close($con);


and it gave me the "echo" Save Failed., is there something wrong with the connection.?
Was This Post Helpful? 0
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Problem in update

Posted 15 January 2013 - 09:02 PM

If it was the connection line 4 should have caught it (mysql_connect returns false on failure). My guess is it's something wrong with the query. You can echo mysql_error and it will give you the error from the previous query.

Also, look into PDO/MySQLi and proper html/php separation.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Problem in update

Posted 15 January 2013 - 09:20 PM

Please read my posts. If you aren't taking our advice, you are going to have a harder time fixing your issues.
Was This Post Helpful? 1
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in update

Posted 15 January 2013 - 09:24 PM

Well, the problem is that you need to test the query like Mac says...once you do that you will know where exactly the error lies.

regards,
Raghav

This post has been edited by raghav.naganathan: 15 January 2013 - 09:26 PM

Was This Post Helpful? 1
  • +
  • -

#7 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem in update

Posted 15 January 2013 - 09:26 PM

View Postraghav.naganathan, on 15 January 2013 - 09:24 PM, said:

Well, the problem is that you have used $con in one place and $conn in another place.

regards,
Raghav


everything is replaced by $con now still update failed.
Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in update

Posted 15 January 2013 - 09:28 PM

View PostJheroll, on 16 January 2013 - 09:56 AM, said:

View Postraghav.naganathan, on 15 January 2013 - 09:24 PM, said:

Well, the problem is that you have used $con in one place and $conn in another place.

regards,
Raghav


everything is replaced by $con now still update failed.


No sorry, I misread your code....ignore that....replace the $con and $conn as they were previously.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in update

Posted 15 January 2013 - 09:39 PM

Well,try if this works.


$sql="UPDATE usersinfo SET LASTNAME='$MODlastname', FIRSTNAME='$MODfirstname', MIDDLENAME='$MODmiddlename', PROJECT='$MODproject', DEPARTMENT='$MODdepartment', POSITION='$MODposition' WHERE ID='$MODid'";
mysql_query($sql) or die("MySQL Error: " . mysql_error());


Edit: Thank you creativecoding

I forgot to add that 'die'...editing now...

regards,
Raghav

This post has been edited by raghav.naganathan: 15 January 2013 - 09:46 PM

Was This Post Helpful? 1
  • +
  • -

#10 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Problem in update

Posted 15 January 2013 - 09:39 PM

Are you even reading my or macosxnerd101's replies? There's literally a function built in that will tell you what's wrong in your code. It /literally/ tells you in English where the problem is. It cannot get any simpler.

This post has been edited by creativecoding: 15 January 2013 - 09:40 PM

Was This Post Helpful? 3
  • +
  • -

#11 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem in update

Posted 15 January 2013 - 09:54 PM

tO EVERYONE

raghav, Macosnerd101 and creativecoding

Thank you for your advice Im reading them one by one. And really trying to understand them. My problem was that I dont know anything about PHP and I needed to do this simple functions.

Its working now. Thank you for reading and giving time to my issue. Thank you so much. :)
Was This Post Helpful? 0
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in update

Posted 15 January 2013 - 10:03 PM

Glad I could give my 2 cents...but it is macosxnerd101 who is to be mainly thanked. It was his suggestion which helped you out :)

creativecoding and I just reiterated what he said.

regards,
Raghav

This post has been edited by raghav.naganathan: 15 January 2013 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#13 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem in update

Posted 15 January 2013 - 10:13 PM

View Postraghav.naganathan, on 15 January 2013 - 10:03 PM, said:

Glad I could give my 2 cents...but it is macosxnerd101 who is to be mainly thanked. It was his suggestion which helped you out :)/>

creativecoding and I just reiterated what he said.

regards,
Raghav


still thank you..
I dont even know where to put this
mysql_query($sql) or die("MySQL Error: " . mysql_error());

code until you posted it.

macosxnerd101 thank you :)
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Problem in update

Posted 15 January 2013 - 10:14 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#15 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in update

Posted 15 January 2013 - 10:29 PM

Well, happy to help you out :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1