Jump to content

Passing values using a form *SOLVED*


crzyman

Recommended Posts

This is the form:
[code]
<?
$myValue = "Joe";
?>
<form action="test.php" method="post">
<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100'>
<input type="hidden" name="artist" value="<?php echo $myValue; ?>">
<input type="submit" name="submit" value="submit">
</form>
[/code]

This is the php script test.php:
[code]
<?
//the host, name, and password for your mysql
mysql_connect("localhost","name","pass");

//select the database
mysql_select_db("news");

$artist_name =  $_GET['artist'];

if($submit)
{
   $time=date("h:ia d/j/y");

   $result=MYSQL_QUERY("INSERT INTO shoutbox (id,artist_name,name,message,time)".
      "VALUES ('NULL','$artist_name','$name','$message','$time')");
}
?>
[/code]

Every thing gets put into the database except for the value of $name. Can someone please tell me what I am doing wrong? Many thanks.
I aslo tried this, but still no luck.
[code]
<?
$myValue = "Joe";
?>
<form action="test.php" method="post">

<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100'>
<input type="hidden" name="artist" value=".$myValue.">
<input type="submit" name="submit" value="submit">
</form>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/20867-passing-values-using-a-form-solved/
Share on other sites

I don't understand the line in the form code that defines $myName as Joe, but here's how I'd do the script that adds to your database:

[code]<?php
//the host, name, and password for your mysql
mysql_connect("localhost","name","pass");

//select the database
mysql_select_db("news");
$name =  trim(strip_tags($_POST['name']));
$message = trim(strip_tags($_POST['message']));
$artist_name =  trim(strip_tags($_POST['artist']));

if(isset($_POST['submit'))
{
  $time = date("h:ia d/j/y");
  $query = "INSERT INTO shoutbox (id,artist_name,name,message,time)".
      "VALUES ('NULL','$artist_name','$name','$message','$time')";
  $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
}
?>[/code]

As a side note, you're making future trouble for yourself by entering the time in that format because is you ever want to sort by date, or compare dates you'll have big problems.  Use the datetime yyyy-mm-dd hh:mm format for time in going into the database. You can always reformat that to [b]display[/b] in your preferred format.

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.