0

http://jsfiddle.net/Fd9wx/ I made this to help solve my problem

so I have some php code and html code that should send sql Query's to the database upon the html table I have created like to set up new databases but then I fill out my form and click run it does not want to work for me. I did some google research and got nothing back now before you say "use PDO and This is no longer supported" PDO is hard for me to use because I dont understand some of it I will use it later on but not now, also I did make this script here from hand so dont say "contact script dev" if some one could point me in right direction to solving my problem or just way to make my sql errors show in my script? like the line what to remove and all

here is main part of my script

$tablename=$_POST['tablename'];
$value=$_POST['value'];
$type=$_POST['type'];
$length=$_POST['length'];
$collation=$_POST['collation'];
$attributes=$_POST['attributes'];
$null=$_POST['null'];
$extra=$_POST['extra'];


// Insert data into mysql 
$sql="CREATE TABLE  `a7972613_db`.`$tablename` (
`field1` $type( $length ) $null $extra
) ENGINE = MYISAM";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
}

else {
echo "Please Go Back And Check Your Errors!";
}

thats my main part

Bkay
  • 177
  • 2
  • 4
  • 16
Jacob Anthony Tonna
  • 515
  • 3
  • 13
  • 27
  • Please Go Back And Check Your Errors! <~ my preset error is the error that shows – Jacob Anthony Tonna Jan 21 '13 at 09:16
  • use `echo mysql_error();` to get the error message. – N.B. Jan 21 '13 at 09:23
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 24 '13 at 12:59

2 Answers2

0

The problem with your code is you have not selected the database.

$host = "xxxxx";
$database = "xxxxx";
$user = "xxxx";
$password = "xxxxx";

// Connect to server and select database.
mysql_connect("$host", "$user", "$password")or die("cannot connect"); 

Use below code for selecting database

// Connect to server and select database.
$conn = mysql_connect("$host", "$user", "$password")or die("cannot connect"); 
mysql_select_db($database,$conn);

and another problem is when your query fails, you have hardcoded the error,but use below code for checking where is the problem in your query

$result=mysql_query($sql) or die(mysql_error());
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • i made this and ran it off the db directly and it works look at my jsfiddle my tablename is not being sent thrue im trying to run Querys on the database not table CREATE TABLE `a7972613_db`.`1236` ( `field1` VARCHAR( 15 ) NOT NULL ) ENGINE = MYISAM – Jacob Anthony Tonna Jan 21 '13 at 09:40
0

Change your query to

$result = mysql_query($sql) or die("Error with $sql: " . mysql_error());

with mysql_error(), you will see what your problem is.

You can dump your $sql string in order to see, whether it is correct

echo $sql;
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • @user1978141 You get `$tablename` from your `$_POST` data. Enter a correct table name in your form and try again. – Olaf Dietsche Jan 21 '13 at 09:30
  • @user1978141 `1236` is not a valid table name. Use a name starting with a letter. – Olaf Dietsche Jan 21 '13 at 09:50
  • Querys on the database not table, The Query gets make with the html and sent to the php the php runs it on database not table its a script to make a database without logging into the sql mamager – Jacob Anthony Tonna Jan 21 '13 at 09:54
  • @user1978141 You wrote `create table a7972613_db.1236 ...` as a comment. Use something like `create table a7972613_db.a1236 ...` for example. – Olaf Dietsche Jan 21 '13 at 09:56