以下是代码:
<?php
$website = $_POST['valorCaja1'];
$enlace_name = $_POST['valorCaja2'];
$link = $_POST['valorCaja3'];
include("connection.php");
if (!$website || !$enlace_name || !$link) {
echo 'No existen las variables';
}else{
$sql = "INSERT INTO images (enlace, enlace_name, website, Created) VALUES (:link, :enlace_name, :website, NOW())";
$query = $handler->prepare($sql); // Here is the problem :(
$query->execute(array(
':link' => $link,
':enlace_name' => $enlace_name,
':website' => $website
));
echo $handler->lastInsertId();
$resultado = 'El Enlace ha sido registrado exitosamente!!';
echo $resultado;
}
?>
我已经做了一切来解决它,但我无法解决。
答案 0 :(得分:2)
"The strange is that when I put the variables direct in that file, I mean that the file works great, but when I pass variable from one page to another happens this!!"
Chances are, your form's input elements do not contain "name" attributes, or that they were mispelled, and/or that your form does not specify a POST method.
I.e.:
<form method="post" action="your_handler.php">
Website:
<input type="text" name="valorCaja1">
Enlace name:
<input type="text" name="valorCaja2">
Link:
<input type="text" name="valorCaja3">
<input type="submit" name="submit" value="Submit">
</form>
Error checking methods: (consult footnotes also)
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
right after the connection is opened, if you're not already doing so.Make sure you are indeed using PDO to connect with, and not mysqli_
.
Those different MySQL APIs do not intermix with each other.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Footnotes:
Instead of if (!$website || !$enlace_name || !$link)
, use a conditional empty()
.
if(
!empty($_POST['valorCaja1'])
&&
!empty($_POST['valorCaja2'])
&&
!empty($_POST['valorCaja3'])
)
{
$website = $_POST['valorCaja1'];
$enlace_name = $_POST['valorCaja2'];
$link = $_POST['valorCaja3'];
...
}
$query = $handler->prepare($sql); // Here is the problem :(
I.e.:
$handler = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
Reference(s):