Jump to content

(Newbie question)Simple read and write.


Roy766

Recommended Posts

Well, I decided to try and write a simple script to measure how bad I am at MySQL.

 

Apparently, very bad.

 

My idea is simple: You type text into a textbox, it redirects you to a different page, at which the data is sent to the database, and then all of the data is echoed on the main page. Problem is, it doesn't work, and I'm not experienced enough to determine why. All I get when I try to echo is "resource id #2". Here's my code:

 

Main page (forum.php)

<html>
<head>
<title>Forum thingy....</title>
</head>
<body>
<?php
mysql_connect("mysql4.freehostia.com", "*******", "******") or die(mysql_error());
mysql_select_db("aiddre_db") or die(mysql_error());

$result = mysql_query("SELECT * FROM royforum_list");   

while($row = mysql_fetch_assoc($result)){
   echo $result;
}
?>
<form name="submitcomment" method="post" action="submitcomment.php">
<input type="text" name="commentval">
<input type="submit" value="Submit">
</body>
</html>

 

Redirected page (submitcomment.php):

<?php
    mysql_connect("mysql4.freehostia.com", "aiddre_db", "mioki766") or die(mysql_error());
    mysql_select_db("aiddre_db") or die(mysql_error());
    $commentvalue = $_POST['commentval'];
    $sql = "INSERT INTO royforum_list (comment) VALUES ('$commentvalue')";
    $result = mysql_query($sql);
     echo "Comment posted. Please <a href='forum.php'>return to the forum-ish thing.</a>"
?>

 

Any suggestions?

 

Link to comment
Share on other sites

This part in your script take the result set it gets from the database, and places it into an array. Change the while part in your first script to the following:

 

while($row = mysql_fetch_assoc($result)){
    echo $row['column'];
}

 

Notice the $row['column'] part. The row is the current record in the loop (thanks to the while loop) and the bit in the [ ] brackets says what column from that record you want to reference.

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.