Jump to content

insert PHP + MySQL data into a form


Ippox

Recommended Posts

Hi.

 

I'm making a private webpage for my self where i can post news, games, music titles and movies.

I'm made it so that i can post and delete them from my page.

Also i wanted to have the option to edit them.

 

So i made a action that shows me a list og the posts in each category.

And then i want the option to hit UPDATE, and then it should open the form with the info's from MySQL.

 

I've made those two options as the following:

 

elseif ( $action == "EditNews" ) {
// EDIT NEWS START
if ( !$_GET['id'] ) {
?>
<div id="content">

<?php
$sql = mysql_query("SELECT * FROM news") or die(mysql_error());
while ( $row = mysql_fetch_assoc($sql) ) {
echo "<a href=\"?page=Admin&action=UpdateNews&id=" . $row['news_id'] . "\">Update</a>  | " . $row['subject'] . "<br>";
}
echo "</div>";

} else {
$type = $_GET['type'];
$id = $_GET['id'];
}
// EDIT NEWS END

} elseif ( $action == "UpdateNews" ) {
// UPDATE NEWS START
if ( !$_POST['submit'] ) {
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error());

$user_id = $_SESSION['uid'];
$subject = $_row['subject'];
$body = nl2br($_row['body']);

?>
<div id="content">
<form action="?page=Admin&action=EditNews" method="post">
<div id="">Subject</div>
<div id=""><input id="input" type="text" name="subject"></div>
<div id="">Message</div>
<div id=""><textarea id="input" name='body' cols='15' rows='4'></textarea></div>
<div id=""><input id="submit" class="input" type="submit" name="submit" value="Post News"></div>
</form>
</div>
<?php
} else {
?>
<div id="content">Not Working!</div>
<?php
}
// UPDATE NEWS END
} 

 

- I know i miss something, but i've tried to get to the point, where i only have the form as blank, and need some help from there.

I hope someone can help me with my problem.

 

MOD EDIT:

 . . . 

tags added.

Link to comment
Share on other sites

The problem is that i want to get the MySQL data from that specific post, into the form.

And i know that i have to use <?php echo " .$row['subject'] . " ?> as value of the subject input.

And <?php echo " .$row['body'] . " ?> in the textarea.

 

But when i add it there, the page goes blank.

and it doesn't give any errors at all either.

 

So my problem is that i want my action "UpdatePost" to be able to let my form appear with the informations from the posted informations from MySQL.

But i can't seems to figure out what i need to do.

Link to comment
Share on other sites

before you can use $row["column"] you have to use mysql_fetch_assoc(result of mysql_query), which will give you an array of your result. You are already doing this in the while loop you use to display the link, but not for the form. Thus, you should insert

$row_to_update = mysql_fetch_assoc($sql);

after this

$sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error());

and then fill the form like this

echo $row_to_update[column_name];

Link to comment
Share on other sites

Just tried it.

 

So my action for UpdateNews looks like this:

 

elseif ( $action == "UpdateNews" ) {
  // UPDATE NEWS START
if ( !$_POST['submit'] ) {
	$id = $_GET['id'];
	$row_to_update = mysql_fetch_assoc($sql);
	$sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error());


?>
	<div id="content">
		<form action="?page=Admin&action=EditNews" method="post">
			<div id="">Subject</div>
			<div id=""><input id="input" type="text" name="subject" value='<?php echo " $row_to_update[subject] "; ?>'></div>
			<div id="">Message</div>
			<div id=""><textarea id="input" name='body' cols='15' rows='4'></textarea><?php echo " $row_to_update[body] "; ?></div>
			<div id=""><input id="submit" class="input" type="submit" name="submit" value="Post News"></div>
		</form>
	</div>
<?php
}	else {
?>		
		<div id="content">
			Hmm.. Noget fungere ikke som det skal!
		</div>
<?php	
	}			
  // UPDATE NEWS END
  }

 

And now it can find the form. But its still blank.

Its like it can't get the information from my MySQL database.

Link to comment
Share on other sites

Ohh.. Changed it, and now it works :)

Now i only have to check if i can "submit" the post successfully.

 

Can you guys double check for me, if its correct?

I will be able to use the "submit" function again.. right?

 

Link to comment
Share on other sites

Okay.. After tried a bit, i keep getting this error when pressing on "Update Post":

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('1', 'Testing', 'Test', 'Now()')' at line 1

 

My following code is the following:

elseif ( $action == "UpdateNews" ) {
  // UPDATE NEWS START
if ( !$_POST['submit'] ) {
	$id = $_GET['id'];

	$sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error());
	$row_to_update = mysql_fetch_assoc($sql);

?>
	<div id="content">
		<form action="?page=Admin&action=UpdateNews" method="post">
			<div id="">Subject</div>
			<div id=""><input id="input" type="text" name="subject" value='<?php echo"$row_to_update[subject]"; ?>'></div>
			<div id="">Message</div>
			<div id=""><textarea id="input" name='body' cols='15' rows='4'><?php echo"$row_to_update[body]"; ?></textarea></div>
			<div id=""><input id="submit" class="input" type="submit" name="submit" value="Update News"></div>
		</form>
	</div>
<?php
}	else {
			$user_id = $_SESSION['uid'];
			$subject = $_POST['subject'];
			$body = nl2br($_POST['body']);

			$errors = array();

			if ( !$user_id ) {
			  $errors[1] = "You are not logged in.";
			}
			if ( !$subject ) {
			  $errors[2] = "You did not type a subject.";
			}
			if ( !$body ) {
			  $errors[3] = "You did not type a message.";
			}

			if ( count($errors) > 0 ) {
			  
			  echo "<ul>";
			  foreach ( $errors as $error ) {
				echo "<li>";
				echo $error;
				echo "</li>";
			  }
			  echo "</ul>";
			} else {
?>				
		<div id="content">
<?php				
			  $sql = mysql_query("UPDATE news SET('$user_id', '$subject', '$body', 'Now()')") or die(mysql_error());

			  echo "You have updated the news successfully!";
			  echo "<br/>";
			  echo "<a href=\"?page=Admin&action=EditNews\">Update More</a>";
			}
			  echo "</div>";
?>
<?php	
	}			
  // UPDATE NEWS END
  }

 

Can anyone see where my problem is?

Link to comment
Share on other sites

I've changed it a bit.

And it reacts to the Update Post botton.

Except that it doesn't UPDATE the changes.

 

What have i done wrong?

 

elseif ( $action == "UpdateNews" ) {
  // UPDATE NEWS START
if ( !$_POST['submit'] ) {
	$id = $_GET['id'];

	$sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error());
	$row = mysql_fetch_assoc($sql);

?>
	<div id="content">
		<form action="?page=Admin&action=UpdateNews" method="post">
			<div id="">Subject</div>
			<div id=""><input id="input" type="text" name="subject" value='<?php echo"$row[subject]"; ?>'></div>
			<div id="">Message</div>
			<div id=""><textarea id="input" name='body' cols='15' rows='4'><?php echo"$row[body]"; ?></textarea></div>
			<div id=""><input id="submit" class="input" type="submit" name="submit" value="Update News"></div>
		</form>
	</div>
<?php
}	else {
			$user_id = $_SESSION['uid'];
			$subject = $_POST['subject'];
			$body = ($_POST['body']);

			$errors = array();

			if ( !$user_id ) {
			  $errors[1] = "You are not logged in.";
			}
			if ( !$subject ) {
			  $errors[2] = "You did not type a subject.";
			}
			if ( !$body ) {
			  $errors[3] = "You did not type a message.";
			}
			if ( count($errors) > 0 ) {
			  
			  echo "<ul>";
			  foreach ( $errors as $error ) {
				echo "<li>";
				echo $error;
				echo "</li>";
			  }
			  echo "</ul>";
			} else {
?>				
		<div id="content">
<?php				
			  $sql = mysql_query("UPDATE news SET user_id='$user_id', subject='$subject', body='$body', date='Now()' WHERE news_id='$id'") or die(mysql_error());

			  echo "You have updated the news successfully!";
			  echo "<br/>";
			  echo "<a href=\"?page=Admin&action=EditNews\">Update More</a>";
			}
			  echo "</div>";
?>
<?php	
	}			
  // UPDATE NEWS END
  }

Link to comment
Share on other sites

Remove the query string from the query execution and assign it to a variable, then use the variable in the execution. Echo the query string and mysql_error() if the query fails.

 

$query = "UPDATE query here";
if( $result = mysql_query($query) ) {
     if( mysql_affected_rows() < 1 ) {
          echo 'Number of rows updated is 0!';
     }
} else {
     echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>';
}

Link to comment
Share on other sites

Thanks for your reply.. Its working now.

I tried to echo out the $sql and noticed that it didn't get the used news_id to update.

So it couldn't figure out where to UPDATE the infos.

 

Anyways, the problem was that no variable was set for the news_id as in the POST action..

So now its working smoothly.

 

Thanks everyone for your help :)

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.