Jump to content

2 Quick Problems! Posting info from drop down box?


Deanznet

Recommended Posts

Okay

 

I got a drop down box and it grabs tables from mysql and list. now i need to post from it. heres the code

 

<?php
$sql = "SHOW TABLES FROM $db_name";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

echo "<select name=Field>";

while ($row= mysql_fetch_row($result)) {

echo "<option value={$row[0]}>{$row[0]}</option>";

}
echo "</select>";
?>

 

That works fine but  $cat = $_POST['Field']; Dose not work.. Understand?

 

Second problem is my php script to create Tables in mysql works only with single words so if i do Cow it will work but if i do Big Cow i get this error.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Cow ( `id` int(255) NOT NULL default '0', `Product` varchar(32) NOT NULL d' at line 1

 

<?
include("conn.php");

if($_POST['submit']) //If submit is hit

{

$name= $_POST['catname'];

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE IF NOT EXISTS  $name (
  `id` int(255) NOT NULL default '0',
  `Product` varchar(32) NOT NULL default '',
`Cat` varchar(32) NOT NULL default '',
  `Info` text NOT NULL,
  `Paypal code` text NOT NULL,
  `Img1` varchar(50) NOT NULL default '',
  `Img2` varchar(50) default NULL,
  `Img3` varchar(50) default NULL,
  `Img4` varchar(50) default NULL,
  `Img5` varchar(50) default NULL,
  `Img6` varchar(50) default NULL
)")
or die(mysql_error());  
}

header("Location: newcat.php"); 
exit; 
?>

You need to put your select boxes in a form and send to a processer like your second set of code. I would put them all on the same page like the following.

 

 

<?php
include("conn.php");

if($_POST['submit']) //If submit is hit proccess the form else display the form

{

$name= $_POST['catname'];

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE IF NOT EXISTS  $name (
  `id` int(255) NOT NULL default '0',
  `Product` varchar(32) NOT NULL default '',
`Cat` varchar(32) NOT NULL default '',
  `Info` text NOT NULL,
  `Paypal code` text NOT NULL,
  `Img1` varchar(50) NOT NULL default '',
  `Img2` varchar(50) default NULL,
  `Img3` varchar(50) default NULL,
  `Img4` varchar(50) default NULL,
  `Img5` varchar(50) default NULL,
  `Img6` varchar(50) default NULL
)")
or die(mysql_error()); 
}
else
{

$sql = "SHOW TABLES FROM $db_name";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
echo "<form action=".$_SERVER['PHP_SELF'].">"; // Here is where your the form starts and defines where to process the input. 
echo "<select name='Field'>";

while ($row= mysql_fetch_row($result)) {

echo "<option value={$row[0]}>{$row[0]}</option>";

}
echo "</select>";

//Now lets add a Submit button and close the form.
echo "															
<input type='submit' value='Submit' name='Submit'> 
</form>
"; 
}
?>

Ya i understand but you mixing the 2 problems up..

 

First problem is have a drop down and i need help getting the information from the post.

 

You know if i have a text box names catname and i hit submit to go to process php i will have  $name= $_POST['catname'];

 

so i can echo $name and it will echo what ever was posted..

 

How do i do that with the drop down box?

I dont know what I was thinking that code wouldnt even post the info into sql anyway

 

Change this

$name= $_POST['catname'];

 

To

$name= $_POST['Field'];

 

 

OR

 

Change this

echo "<select name='Field'>";

 

to

echo "<select name='catname'>";

 

 

Final code should be this if you changed the select name to "catname"

 

 

<?php

if($_POST['submit']) //If submit is hit proccess the form else display the form

{

$name= $_POST['catname'];

  echo "Name = ".$name;
}
else
{

$sql = "SHOW TABLES FROM $db_name";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
echo "<form action=".$_SERVER['PHP_SELF'].">"; // Here is where your the form starts and defines where to process the input. 
echo "<select name='Field'>";

while ($row= mysql_fetch_row($result)) {

echo "<option value={$row[0]}>{$row[0]}</option>";

}
echo "</select>";

//Now lets add a Submit button and close the form.
echo "                                             
<input type='submit' value='Submit' name='submit'> 
</form>
"; 
}
?>

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.