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
https://forums.phpfreaks.com/topic/95706-really-basic-phpmysql-question/
Share on other sites

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.