Jump to content

Most likely simple fix, script doesn't update MySQL query.


Clandestinex337

Recommended Posts

Hello, I am trying to pick up php again and just exercising my skills. So I have it so that it fills my form with the values of what I want to edit, and when I click the edit button, it doesn't edit any of the information. When I echo out $result, I get a MYSQL query string that has the same values as the table, so its not getting the new values that are edited.

 

<?php

@mysql_connect('localhost', 'root', '') or die("Could not connect to Mysql Server. " . mysql_error());
@mysql_select_db('tutorials') or die("Could not connect to Database. " . mysql_error());

if(isset($_GET['edit']))
{
	$id = $_GET['edit'];
	$query = "SELECT `username`, `password` FROM `users` WHERE `id` = '$id'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$name = $row['username'];
	$password = $row['password'];		
}

if(isset($_POST['edit']))
{
	$id = $_GET['edit'];
	$query = "UPDATE `users` SET `username` = '$name', `password` = '$password' WHERE `id` = '$id'";
	$result = mysql_query($query);

	echo $query;

	if(!$result)
	{
		echo mysql_error();
	}else{
		echo 'updated post';
	}	
}
?>
<form method="POST" action="" >
<input type="text" name="name" value="<?php echo $name; ?>" /> First name <br />
<input type="text" name="password" value="<?php echo $password; ?>" /> Last name <br />
<input type="submit" name="edit" value="edit" />
</form>

 

I believe it has something to do with the values of $name and $password in the form conflicting with the first if isset and the second if isset.

 

Thanks for any help possible  8)

Link to comment
Share on other sites

<?php

@mysql_connect('localhost', 'root', '') or die("Could not connect to Mysql Server. " . mysql_error());
@mysql_select_db('tutorials') or die("Could not connect to Database. " . mysql_error());

if(isset($_GET['edit']))
{
	$id = $_GET['edit'];
	$query = "SELECT `username`, `password` FROM `users` WHERE `id` = '$id'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$name = $row['username'];
	$password = $row['password'];		
}

if(isset($_POST['edit']))
{
	$id = $_GET['edit'];
	$query = "UPDATE `users` SET `username` = '$name', `password` = '$password' WHERE `id` = '$id'";
	mysql_query($query);



?>
<form method="POST" action="" >
<input type="text" name="name" value="<?php echo $name; ?>" /> First name <br />
<input type="text" name="password" value="<?php echo $password; ?>" /> Last name <br />
<input type="submit" name="edit" value="edit" />
</form>

 

 

Note the edits done at the last if(isset($_POST[edit])) ....

Link to comment
Share on other sites

<?php

@mysql_connect('localhost', 'root', '') or die("Could not connect to Mysql Server. " . mysql_error());
@mysql_select_db('tutorials') or die("Could not connect to Database. " . mysql_error());

if(isset($_GET['edit']))
{
	$id = $_GET['edit'];
	$query = "SELECT `username`, `password` FROM `users` WHERE `id` = '$id'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$name = $row['username'];
	$password = $row['password'];		
}

if(isset($_POST['edit']))
{
	$id = $_GET['edit'];
	$query = "UPDATE `users` SET `username` = '$name', `password` = '$password' WHERE `id` = '$id'";
	mysql_query($query);



?>
<form method="POST" action="" >
<input type="text" name="name" value="<?php echo $name; ?>" /> First name <br />
<input type="text" name="password" value="<?php echo $password; ?>" /> Last name <br />
<input type="submit" name="edit" value="edit" />
</form>

 

 

Note the edits done at the last if(isset($_POST[edit])) ....

 

 

Make sure you close off the last if-statement... Forgot the last } in my edit..

 

Hope it helps

Link to comment
Share on other sites

If that's what you believe the problem may be, try changing those variable names.

I would, but if I change the variables in the form, wont that conflict with the first if statement and not show the items I want edited in the form?

 

 

Note the edits done at the last if(isset($_POST[edit])) ....

 

Sorry for my ignorance, but I do not understand what you are trying to point at.

 

Thanks ;)

Link to comment
Share on other sites

If that's what you believe the problem may be, try changing those variable names.

I would, but if I change the variables in the form, wont that conflict with the first if statement and not show the items I want edited in the form?

 

 

Note the edits done at the last if(isset($_POST[edit])) ....

 

Sorry for my ignorance, but I do not understand what you are trying to point at.

 

Thanks ;)

 

 

pointing at the last if(isset( ... in your script. at the bottom of the page before the closing php ?>

compare your script with the one i edited and youll find it with ease

Link to comment
Share on other sites

The variables can have different names for different purposes.  One is "name from the db" and "password from the db".  The other is "name from the form" and "password from the form".  So:

 

<?php

@mysql_connect('localhost', 'root', '') or die("Could not connect to Mysql Server. " . mysql_error());
@mysql_select_db('tutorials') or die("Could not connect to Database. " . mysql_error());

if(isset($_GET['edit']))
{
	$id = $_GET['edit'];
	$query = "SELECT `username`, `password` FROM `users` WHERE `id` = '$id'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$db_name = $row['username'];
	$db_password = $row['password'];		
}

if(isset($_POST['edit']))
{
	$id = $_GET['edit'];
	$query = "UPDATE `users` SET `username` = '$name', `password` = '$password' WHERE `id` = '$id'";
	$result = mysql_query($query);

	echo $query;

	if(!$result)
	{
		echo mysql_error();
	}else{
		echo 'updated post';
                        $db_name = $name;
                        $db_password = $password;
	}	
}
?>
<form method="POST" action="" >
<input type="text" name="name" value="<?php echo $db_name; ?>" /> First name <br />
<input type="text" name="password" value="<?php echo $db_password; ?>" /> Last name <br />
<input type="submit" name="edit" value="edit" />
</form>

 

The two changes are to use $db_name and $db_password for the data fetched from the database, and also to overwrite the db variables when an updated name and password is written to the database.

Link to comment
Share on other sites

The variables can have different names for different purposes.  One is "name from the db" and "password from the db".  The other is "name from the form" and "password from the form".  So:

 

The two changes are to use $db_name and $db_password for the data fetched from the database, and also to overwrite the db variables when an updated name and password is written to the database.

 

I made theses changes, but it updates the query with empty values.

Link to comment
Share on other sites

Ok, I had assumed you had the old setting to make $_POST values turn magically into local variables on.  But if you don't, you will need to use $_POST['name'] and $_POST['password'] when you want to refer to the data from the form.  An easy way to do this is to add this code inside the second "if":

 

$name = $_POST['name'];
$password = $_POST['password'];

 

Once you've got it all working, you might want to consider using mysql_real_escape_string() on your variables that go into the database.  This is essential if this script is going to be accessible on the internet, as otherwise people will be able to take control of your database.  But I would get it working properly first.

Link to comment
Share on other sites

Ok, I had assumed you had the old setting to make $_POST values turn magically into local variables on.  But if you don't, you will need to use $_POST['name'] and $_POST['password'] when you want to refer to the data from the form.  An easy way to do this is to add this code inside the second "if":

 

Thanks that worked!

 

Instead of making a new post, I have a quick question. When I have my code delete one of the post, it redirects to the delete.php file which runs the query to delete the post, but I had to make a link that you click and it goes to the index.php file. Is there a way that when it goes to the delete.php file it redirects to the index.php file automatically?

 

I thought I saw something like this before, I think it was called header?

 

Thanks a lot.

Link to comment
Share on other sites

You can do it like this:

 

header("Location: /index.php");
exit(0);

 

The biggest issue here is if you want to display a notification that the post was deleted.  For this reason you may want to use meta refresh instead:

 

http://en.wikipedia.org/wiki/Meta_refresh

 

This lets you set a delay of a few seconds before the redirection, which can be better than the header redirect, which redirects without displaying anything.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.