Jump to content

[SOLVED] $_REQUEST not pulling through variable


englishcodemonkey

Recommended Posts

Ok so i have a form that contains several text fields and a drop down field.  The HTML for the form is below:

 

<form action="insert.php" method="post" >

  <p align="left">
  Hosta ID:
    <input name="id" type="text" id="id" />  <-- leave blank!! </p>

  <p align="left">Hosta Name: 
    <input name="name" type="text" value="" />
  </p>
  <p align="left">
    Hybridizer: 
    <input name="hybrid" type="text" id="hybrid"/>
  </p>
  <p align="left">
    Size: 
    <select name="size" id="size">
      <option value="miniature" selected="selected">Miniature</option>
      <option value="dwarf">Dwarf</option>
      <option value="small">Small</option>
      <option value="medium">Medium</option>
      <option value="large">Large</option>
      <option value="giant">Giant</option>
    </select>
  </p>
  <p align="left">Description: 
    <textarea name="desc" cols="100" rows="4" id="desc"></textarea>
  </p>
    <p>Price: 
      <input type="text" name="price" />
  </p>
    <p> </p>
    <p>
      <input type="submit" name="send" />
  </p>
</form>

 

And it is posting to insert.php the code is below:

 


<?php

$dbhost ='p50mysql187.secureserver.net';
$dbuser = 'hostajess';
$dbpass = '****';

$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die('Error connecting to mysql');

$dbname='hosta';
mysql_select_db($hosta);

$name = $_POST['name'];
$hy = $_POST['hybrid'];
$size = $_POST['size'];
$desc= $_POST['desc'];
$price= $_POST['price'];	
echo '<h1>Size is $size</h1>';

$sql = 'INSERT INTO hosta VALUES (\'$hid\', \'$name\', \'$hy\', \'$size\', \'$desc\', \'$price\')';
$r= @mysqli_query ($dbc, $sql);
if ($r) {
echo '<h1>Thank You! SUCCESS!</h1>';
} else {
echo '<h1>System Error</h1>';
echo '<p>You Entered, <b>$name</b> into the database</p>';
mysqli_close($dbc);

exit();
}

?>

I feel like I Have tried everything, the result I am getting is that the $var is being inserted into the database.  For example the hosta_id will be correct (auto_increment is being used) and then the name size price and everything will show $name, $size, $price. Rather than showing the value I entered into the form.

 

Any ideas are welcome! Thanks

Link to comment
Share on other sites

After removing the /'s it said there was an unknown variable and so I then removed the ' aswell. Then it took the statement but entered the '$name' as text into the database rather than the value I had entered into the form??? I can't understand why it is not recognising this as a variable and is just seeing it as text?? Thanks

Link to comment
Share on other sites

You do not need to parse the php variables in the mysql query. Parsing them is slower and allows room for errors.

 

Use...

$sql = "INSERT INTO hosta VALUES (".$hid.", ".$name.", ".$hy.", ".$size.", ".$desc.", ".$price.")";

 

Even better be more specific in your mysql query, try...

$sql = "INSERT INTO hosta SET *COL1* = '".$val1."', *COL2* = '".$val2."', .....";

where *COL1* is changed for the name of the column!

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.