Member Avatar for Member #910227

this is the code to email confirm. The user clicks on the link in the email, passing the code through url to this page. Then the data is transferred from a tmp table to main one.

I have queried in phpmyadmin and it works fine. But when i actually use the php i get the "else {echo "error";}" suggesting that the INSERT query is not working. Any suggestions?

include("db_config_db.php");

$code=$_GET['code'];

$sql1="SELECT * FROM table1 WHERE code ='$code'";
$result1=mysql_query($sql1);

if($result1){
$count=mysql_num_rows($result1);

if($count==1){

while($rows=mysql_fetch_array($result1)){
$username = $rows['username'];
$name = $rows['name'];
$email = $rows['email'];
$address = $rows['address'];
$city = $rows['city'];
$state = $rows['state'];
$zip = $rows['zip'];
$phone = $rows['phone'];
$birthday = $rows['birthday'];
$country = $rows['country'];
$description = $rows['description'];
$keywords = $rows['keywords'];
$setup = $rows['setup'];
}

$sql2="INSERT INTO table2 (name, email, address, city, state, zip, phone, birthday, country, description, username, code, keywords, setup)VALUES('$name', '$email', '$address', '$city', '$state', '$zip', '$phone', '$birthday', '$country', '$description', '$username', '$code', '$keywords', '$setup')";
$result2=mysql_query($sql2);

if($result2){
$sql3="DELETE FROM table1 WHERE code = '$code'";
$result3=mysql_query($sql3);
header("location:index.php");
}
else{ echo "error";
}
}
else {
echo "Wrong Confirmation code";
}
}

Dani AI

Generated

Since 's SELECT returns a row, the connection and read permissions are fine. The next required step is to get the database to report why the INSERT fails: print the actual INSERT statement and the MySQL error immediately after the failed query so the real cause (syntax, constraint, permission, length, etc.) is visible.

/* quick debug right after the INSERT attempt */
if (!$result2) {
    echo 'MySQL error ['.mysql_errno().']: '.mysql_error() . "\n";
    echo 'Failed query: ' . $sql2 . "\n";
    var_export($rows);   // inspect values fetched from table1
    exit;
}

Common causes to check once the error is known:

  • Unescaped quotes or binary data in a value causing SQL syntax errors — escape inputs or use prepared statements.
  • UNIQUE / NOT NULL constraints, or data too long for a column — check DESCRIBE or SHOW CREATE TABLE table2.
  • Reserved column names or mismatched column names — either wrap columns in backticks or correct names.
  • Missing INSERT privileges for the DB user (SELECT can work while INSERT is disallowed) — check SHOW GRANTS FOR CURRENT_USER().
    Also confirm that fetched fields are scalar strings (use var_dump/var_export) and not arrays or unexpected types.

For reliability and security: stop concatenating raw values into SQL. Migrate to mysqli or PDO and use parameterized queries; when moving rows from a temp table, use transactions so the delete only occurs on a successful commit. As advised, add explicit MySQL error output; as noted, inspect the fetched variables. Once the actual MySQL error text is available, the precise fix will be straightforward.

Recommended Answers

All 8 Replies

Snip im an idiot.. nvm lol

Member Avatar for Member #910227

Not completely sure what you mean, but yes, mysql typically works. The first SELECT is working, but the INSERT INTO is not.

Post ur html file from where u send the info......

do use die function every mysql statement such as
mysql_query("select * from fdf) or die("not working1");
u can able to detect which function is not working....
Tell me if u find any error

commented: good +6

$username = $rows;
$name = $rows;
$email = $rows;
$address = $rows;
$city = $rows;
$state = $rows;
$zip = $rows;
$phone = $rows;
$birthday = $rows;
$country = $rows;
$description = $rows;
$keywords = $rows;
$setup = $rows;
}

make sure ur html textboxes names is same as ur given index name

ex.
$setup = $rows; = <input type=text name="setup">

Member Avatar for Member #910227

i'm not posting to the page through the form. I'm selecting them from the database

Tell me the error you get while parsing in php?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.