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
https://forums.phpfreaks.com/topic/229890-insert-php-mysql-data-into-a-form/
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.

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];

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.

When you say the page is blank, do you mean there is nothing at all displayed, or that the fields simply don't have values?

 

In your php.ini file, do you have error reporting on?

 

error_reporting = -1

display_errors = On

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?

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
  }

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>';
}

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 :)

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.