Hello; I prepared a html form whereby I am trying to create a mysql table. The php page, which gets the variables from the html form, has the following coding //mysql connection and the related coding here //create table as posted via variable $table_name mysql_query("CREATE TABLE $table_name( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $areaA $typeA($charA), //all variable coming from the form $areaB $typeB($charB)") or die(mysql_error()); echo "table created"; PHP: But it doesn't work. Can you tell me what the problem is? Thanks in advance.
As I don't see the complete code ... the question is: is there any error? Is the PHP error reporting turned on? If not turn on and allow to display even Notice errors too.
What you have written seems fine, but the code just sits there ?, to run the code it would be something like this: $sql = "mysql_query("CREATE TABLE $table_name( //$sql = " open the query statement id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $areaA $typeA($charA), $areaB $typeB($charB)") "; // we close the query here mysql_query($sql, $your_connection_code); // run the query echo "table created"; PHP: Might need tweaking...
Well, here is the whole code. It echoes "table created" when worked. However, no table is created. <? include('baglan.php'); //make a mysql connection $tablo_adi = $_POST['tablo_adi']; $alanA = $_POST['alanA']; $turuA = $_POST['turuA']; $karakterA = $_POST['karakterA']; $alanB = $_POST['alanB']; $turuB = $_POST['turuB']; $karakterB = $_POST['karakterB']; $alanC = $_POST['alanC']; $turuC = $_POST['turuC']; $karakterC = $_POST['karakterC']; $tablo_adi = stripslashes($tablo_adi); $tablo_adi = mysql_real_escape_string($tablo_adi); $alanA = stripslashes($alanA); $alanA = mysql_real_escape_string($alanA); $turuA = stripslashes($turuA); $turuA = mysql_real_escape_string($turuA); $karakterA = stripslashes($karakterA); $karakterA = mysql_real_escape_string($karakterA); $alanB = stripslashes($alanB); $alanB = mysql_real_escape_string($alanB); $turuB = stripslashes($turuB); $turuB = mysql_real_escape_string($turuB); $karakterB = stripslashes($karakterB); $karakterB = mysql_real_escape_string($karakterB); $alanC = stripslashes($alanC); $alanC = mysql_real_escape_string($alanC); $turuC = stripslashes($turuC); $turuC = mysql_real_escape_string($turuC); $karakterC = stripslashes($karakterC); $karakterC = mysql_real_escape_string($karakterC); $con = mysql_connect("localhost","username","password"); $db_selected = mysql_select_db('database', $con); $sql = "mysql_query(CREATE TABLE $tablo_adi( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $alanA $turuA($karakterA), $alanB $turuB($karakterB)) "; $result = "mysql_query($sql, $db_selected)"; if ($result) { echo "table created"; } ?> PHP:
$result = "mysql_query($sql, $db_selected)"; if ($result) { PHP: That doesn't really mean anything it will always be true and it doesn't indicate to php to run your command, try running just mysql_query($sql, $db_selected); PHP: ...to see if it creates a table, then you can make some other if statement.
Well, adopting from a working create table example, I simplified the code back to its original with a tiny change and it worked. Can someone please explain why it works when like this: mysql_query("CREATE TABLE $tablo_adi( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $alanA $turuA($karakterA), $alanB $turuB($karakterB), age INT)") //This line is completely irrelevant. I put it there just to see if it would work. Interestingly, it did! or die(mysql_error()); PHP: and doesn't work when like this: mysql_query("CREATE TABLE $tablo_adi( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $alanA $turuA($karakterA), $alanB $turuB($karakterB)") or die(mysql_error()); PHP:
Finally I've got it! One bracket was missing. So, here is the code in its ultimate form: mysql_query("CREATE TABLE $tablo_adi( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $alanA $turuA($karakterA), $alanB $turuB($karakterB))") //one brackey ")" was missing here, right after karakterB or die(mysql_error()); PHP: