Jump to content

Really basic php/mysql question.


cursed

Recommended Posts

Heh, so sorry in advance for asking a stupid question.

 

So this is basically what I want to do:

Have a form submit information to a mySQL database. Another page randomly picks a row and shows it.

 

Heres the code:

(submit.php works perfectly.)

submit.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<? 
//initilize PHP

if($_POST['submit']) //If submit is hit
{
   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("localhost","user","pass"); 

   //select which database you want to edit
   mysql_select_db("user"); 


   //convert all the posts to variables:
   $title = $_POST['title'];
   $message = $_POST['message'];
   $date = $_POST['date'];
   $time = $_POST['time'];
   
   //Insert the values into the correct database with the right fields
   //mysql table = news
   //table columns = id, title, message, who, date, time
   //post variables = $title, $message, '$who, $date, $time
   $result=MYSQL_QUERY("INSERT INTO news (id,title,message,date,time)".
      "VALUES ('NULL', '$title', '$message', '$date', '$time')"); 

    //confirm
   echo "Query Finished"; 
}
else
{
// close php so we can put in our code
?>
<form method="post" action="submit.php">
<TABLE>
<TR>
   <TD>Title:</TD>
   <TD><INPUT TYPE='TEXT' NAME='title' VALUE='Random Update' size=60></TD>
</TR>
<TR>
   <TD>Message:</TD>
   <TD><INPUT TYPE='TEXT' NAME='message' VALUE='' size=60></TD>
</TR><br>

<TR>
   <TD>Date:</TD>
   <TD>
      <!-- You can use PHP functions to automatically get the value of date -->
      <INPUT TYPE='TEXT' NAME='date' VALUE='<? echo date("M.j.y"); ?>' size=60>
   </TD>
</TR>
<TR>
   <TD>Time:</TD>
   <TD>
      <!-- You can use PHP functions to automatically get the value of time -->
      <INPUT TYPE='TEXT' NAME='time' VALUE='<? echo date("g:i a"); ?>' size=60>
   </TD>
</TR>
<TR>
   <TD></TD><br>
   <TD><INPUT TYPE="submit" name="submit" value="submit"></TD> 
</TR>
</TABLE>
</form>

<?
} //close the else statement
?>

</body>
</html>

 

 

show.php

(the part thats not working)

<?php

   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("localhost","user","pass"); 

   //select which database you want to edit
   mysql_select_db("user"); 

$query = "SELECT title,message FROM news ORDER by rand() LIMIT 1";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
echo "<p>" , ($row['entry_title']) , "</p> \n <p>" , nl2br($row['entry_text']) , "</p>";
}

mysql_free_result($result);
mysql_close();
?>

 

 

Thanks in advance for anyone that can 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.