Pregunta

Somehow my query is not working when I click the button. What I'm trying to do is to TRUNCATE the table when the user click the button.

if(isset($_POST['extract'])){

@mysql_query("TRUNCATE TABLE temp");

}

I also tried this:

if(isset($_POST['extract'])){

$myquery = mysql_query("TRUNCATE TABLE temp");

}

Why is it not executing?

This is the html code:

<input type="submit" value="Extract CSV file" name="extract">
¿Fue útil?

Solución

So here is how would approach to debugging this problem:

1) Check if the form is actually submitted. Put this on the top of your PHP file (note: for debugging only)

echo "I have reached my destination";
die();

2) Check if you are submitting via POST or GET. Or easier, replace $_POST with $_REQUEST, which contains both $_POST and $_GET.

if(isset($_REQUEST['extract'])){

3) Ensure the connection to the database is opened.

4) Provide your script the means to tell you if the query executed successfully

$myquery = mysql_query("TRUNCATE TABLE temp") or die("Error: ".mysql_error());

5) Ensure you are looking at the right place for the changes

You would be surprised how easy it is to get confused with multiple databases opened.
Double check the ip address of your DB server, your DB and table name and contents.

One of the above 5 steps will tell you what went wrong.

Otros consejos

Please follow the following code. This will help you.

<?php 
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed"); 
mysql_select_db("DataBase Name")or die("Connection Failed"); 
$temptable = $_POST['temptable']; 
$query = "truncate table $temptable";
 if(mysql_query($query)){ 
      echo "table empty";
  } else{ 
      echo "fail";
  } ?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top