Helder da silveira ventura Helder da silveira ventura - 3 months ago 13
MySQL Question

Php error on ajax call script

I Have Try To Make A Update To A Script That´s Work Before But Now With This Alteration Of Database Reference i can not figure out How To Solve This Error ##
So Wen I try the script it give me this error:


Connected successfully Notice: Undefined index: user_id in
/home/publiadd/public_html/mediaads.eu/villageop/back/saveid.php on
line 34 SQLSTATE[42S02]: Base table or view not found: 1146 Table
'publiadd_registervillageop.publiadd_registervillageop' doesn't exist

$sql = $db->prepare("INSERT INTO publiadd_registervillageop VALUES
(user_id=?)"); $sql->execute(array($user_Id)); //try { //$sql =
"INSERT INTO publiadd_registervillageop (user_id) VALUES ('user_id')";
// $sth = $conn->query($sql); //} catch(PDOException $e) { //echo
$e->getMessage(); //} //$sql = "INSERT
publiadd_registervillageop

//(
user_id
) //VALUES ('publiadd_registervillageop'); //"; //$sql =
mysql_query("INSERT INTO
publiadd_registervillageop
.
users

(
user_id
) VALUES ('user_id');"); //$sql = mysql_query("UPDATE
users
SET user_id = SET user_id +'$user_id' WHERE user_id = " .
$_SESSION['user']); if($sql){ //The query returned true - now do
whatever you like here. echo 'Your ID was saved. Congrats!'; }else{
//The query returned false - you might want to put some sort of error
reporting here. Even logging the error to a text file is fine. echo
'There was a problem saving your points. Please try again later.'; }
}else{ echo 'Your id wasnt passed in the request.'; } // close MySQL
connection $conn = null; ?>


<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors',1);

$servername = "localhost";
$username = "publiadd_publix";
$password = "1a3g7893fsh";

try {
$conn = new PDO("mysql:host=$servername;dbname=publiadd_registervillageop", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}


if(isset($_GET['user_id'])){
//$user_id = intval($_GET['user_id']);
//Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks

try {
$dbh = new PDO("mysql:host=$servername;dbname=publiadd_registervillageop", $username, $password);

$user_id = @$_POST['user_id'];
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO publiadd_registervillageop (user_id)
VALUES ('".$_POST["user_id"]."')";
if ($dbh->query($sql)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}
$dbh = null;
}
catch(PDOException $e){
echo $e->getMessage();
}

}
?>

$sql = $db->prepare("INSERT INTO publiadd_registervillageop VALUES (user_id=?)");
$sql->execute(array($user_Id));
//try {
//$sql = "INSERT INTO publiadd_registervillageop (user_id) VALUES ('user_id')";
// $sth = $conn->query($sql);
//} catch(PDOException $e) {
//echo $e->getMessage();
//}
//$sql = "INSERT `publiadd_registervillageop`
//(`user_id`)
//VALUES ('publiadd_registervillageop');
//";

//$sql = mysql_query("INSERT INTO `publiadd_registervillageop`.`users` (`user_id`) VALUES ('user_id');");
//$sql = mysql_query("UPDATE `users` SET user_id = SET user_id +'$user_id' WHERE user_id = " . $_SESSION['user']);

if($sql){
//The query returned true - now do whatever you like here.
echo 'Your ID was saved. Congrats!';
}else{
//The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
echo 'There was a problem saving your points. Please try again later.';
}
}else{
echo 'Your id wasnt passed in the request.';
}

// close MySQL connection
$conn = null;
?>
<html>
<head>
</head>
<body>
<body bgcolor="#ffffff">
</body>
</html>

Answer

The method you are using in your forms is GET so use $_GET not $_POST as it goes in your if condition. So replace $_POST with $_GET and second error is your table name it is users table not publiadd_registervillageop so your sql query looks like,

$sql = "INSERT INTO users (user_id) VALUES ('".$_GET["user_id"]."')";
Comments