Jump to content

[SOLVED] Frustrating PHP + MySQL- Unknown Character


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

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.