Jump to content

[SOLVED] Frustrating PHP + MySQL- Unknown Character


SamW

Recommended Posts

What's going on? http://www.ballpointstudio.com/form2.php

 

Fill in the form with anything and hit submit. It should display what you entered, as well as every other row, but...instead, it doesn't. This is my code:

<?php
mysql_connect("localhost", "CENSORED", "CENSORED") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("runetyco_cms") or die(mysql_error());
echo "Connected to MySQL<br />";


$content = $_POST['content'];

$result = mysql_query("SELECT * FROM cms")
or die(mysql_error());  

mysql_query("INSERT INTO cms 
(content) VALUES('$content' ) ") 
or die(mysql_error());  


$row = mysql_fetch_array( $result );

echo "Name: ".$row['content'];

?>

The value will not be contained in the array as you are selecting the result set prior to inserting the record. swap the queries around. Should also be an associated array if you are using the field key.

mysql_query("INSERT INTO cms (content) VALUES('$content' ) ") or die(mysql_error());  
$result = mysql_query("SELECT * FROM cms") or die(mysql_error());  
$row = mysql_fetch_assoc($result );

try this:

<?php
mysql_connect("localhost", "CENSORED", "CENSORED") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("runetyco_cms") or die(mysql_error());
echo "Connected to MySQL<br />";


$content = $_POST['content'];

mysql_query("INSERT INTO cms 
(content) VALUES('$content' ) ") 
or die(mysql_error()); 

$result = mysql_query("SELECT * FROM cms")
or die(mysql_error());  



while ($row = mysql_fetch_array( $result ))
{
echo "Name: ".$row['content'];
}

?>

you have a simple error mate

 

From

   mysql_query("INSERT INTO cms 
(content) VALUES('$content' ) ") 
or die(mysql_error());  

 

To:

   mysql_query("INSERT INTO cms 
(content) VALUES("$content") ") 
or die(mysql_error());  

 

From

echo "Name: ".$row['content'];

 

From

echo "Name: ". $row['content'] ."";

 

correct me if i have some errors to... :D

aviavi, mysql_fetch_array() will not return associative keys therefore your code wont work

// bad
$row = mysql_fetch_array($result);
// this index will not exist
print $row['content'];

// good
$row = mysql_fetch_assoc($result);
print $row['content'];

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.