Easy - Help with HTML values from form :c)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theinfamousmickey
Forum Newbie
Posts: 2
Joined: Fri Aug 27, 2010 12:55 pm

Easy - Help with HTML values from form :c)

Post by theinfamousmickey »

Hi guys.

So this is what I'm trying to do.

I have a form on my website,

the user puts in a value and php inserts that value into the database in one field called "name". But I am also trying to use that same value and insert it another field at the same time but just adding "name".".txt". but in a field called "file" (both in the same table of course)

Thank you guys so much in advance! I am new and I really appreciate any help! Thank you!

Here is my field html:

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
 
      <input type="text" name="name" value="" size="32" />
      <input type="submit" value="Do it." />
   
  <input type="hidden" name="numofuser" value="50" />
  <input type="hidden" name="file" value="<?php echo "chatroom-".$name.".txt" ?>" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
       

And I this the PHP portion:

Code: Select all


<?php
   
    session_start();

    require_once("dbcon.php");

    if (checkVar($_SESSION['userid'])):
 
        $getRooms = "SELECT *
                     FROM chat_rooms";
        $roomResults = mysql_query($getRooms);          

?>
<?php require_once('Connections/dbcon.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO chat_rooms (name, numofuser, `file`) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['numofuser'], "int"),
                       GetSQLValueString($_POST['file'], "text"));

  mysql_select_db($database_dbcon, $dbcon);
  $Result1 = mysql_query($insertSQL, $dbcon) or die(mysql_error());

  $insertGoTo = "chatrooms.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_dbcon, $dbcon);
$query_talkabout = "SELECT * FROM chat_rooms";
$talkabout = mysql_query($query_talkabout, $dbcon) or die(mysql_error());
$row_talkabout = mysql_fetch_assoc($talkabout);
$totalRows_talkabout = mysql_num_rows($talkabout);
?>

User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Easy - Help with HTML values from form :c)

Post by Jonah Bron »

Maybe something like this?

Code: Select all

mysql_query(sprintf('INSERT INTO my_table (name, file) VALUES(%s, %s)', $name, $name . '.txt'));
theinfamousmickey
Forum Newbie
Posts: 2
Joined: Fri Aug 27, 2010 12:55 pm

Re: Easy - Help with HTML values from form :c)

Post by theinfamousmickey »

Jonah Bron wrote:Maybe something like this?

Code: Select all

mysql_query(sprintf('INSERT INTO my_table (name, file) VALUES(%s, %s)', $name, $name . '.txt'));

Jonah! You're amazing! Thank you so much. I'm going to try that now.

One question though. How do I take the value from the html field and store it in a variable?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Easy - Help with HTML values from form :c)

Post by Jonah Bron »

Code: Select all

$name = $_POST['name'];
Post Reply