-1

在这里,我通过 JavaScript 从 HTML 页面获取值,从 ajax 访问该值并将该值传递给 PHP 页面。

PHP 页面应该使用该 ajax 值从表中删除,但 PHP 没有获得该 ajax 值...

这是我尝试过的:

JavaScript

var str;
function getResults(a) 
{
       str = a;
}

function showUser() {

    if(str=="") {
        document.getElementById("txtHint").innerHTML="";
        return;
    }

    if(window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","getmovie.php?q="+str,true);
    alert(str);
    xmlhttp.send();
}

PHP

<?php
$q = strtolower(trim($_GET["q"]));
try 
{
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
}
catch (PDOException $e) 
{
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'DELETE FROM movie WHERE LOWER(movie_name) = :q';
$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();
$dbh = null;
?>
4

2 回答 2

1

这个,代码看起来不错,尝试echodelete operation喜欢,

    .......
    ......
    if(!$q)  {// check the movie name,  if empty then return;
       echo 'Movie name is empty';
       return;
    }
    $sql = 'DELETE FROM movie WHERE LOWER(movie_name) = :q';
    $sth = $dbh->prepare($sql);
    $sth->bindValue(':q', $q);
    if($sth->execute())  $msg=$q.' deleted successfully.';
    else $msg=$q.' not deleted.';
    $dbh = null;
    echo $msg;
    return;
?>
于 2013-08-22T05:48:02.737 回答
1

你有没有把 Rohan 给你的代码放在 catch 子句之后

$q = strtolower(trim($_GET["q"]));
$q = strtolower(trim($_GET["q"]));
try 
{
$dbh = new PDO('mysql:dbname=your_database;host=localhost','your_user','your_password');
}
catch (PDOException $e) 
{
echo 'Connection failed: ' . $e->getMessage();
}
if(!$q)  {// check the movie name,  if empty then return;
   echo 'Movie name is empty';
   return;
}
$sql = 'DELETE FROM movie WHERE LOWER(movie_name) = :q';
$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
if($sth->execute())  $msg=$q.' deleted successfully.';
else $msg=$q.' not deleted.';
$dbh = null;
echo $msg;
return;
?>
于 2013-08-22T06:12:15.887 回答