2017-05-05 84 views
-2

我得到一个错误,在我的代码基本上我的用户输入没有被读取,我不知道为什么。我用isset,它跳过了,这意味着用户输入没有被正确读取?任何想法为什么不呢?作为参考,当我提交表单时,我得到另一个错误,说'注意:未定义的变量:绑定在C:\ xampp \ htdocs \ editartist.php' 但我认为这是事实,当你搜索你不再有id了。我留下了一些代码来显示。用户输入不是从HTML格式使用PHP读取

<?php 
if(isset($_GET['id'])) 
{ 
    $artistID = fix_string($_GET['id']); 
    $sql = ("SELECT artName FROM artist WHERE artID = '$artistID' "); 
    $result = $conn->prepare($sql); 
    $result->execute(); 
    $result->bind_result($bound); 
    $result->fetch(); 
} 
else { 
    echo "this is broken"; 
} 
?> 
<form method="post" action="editartist.php"/> 
<?php echo '<input type="text" name="artistname" value= "'.$bound.'">' ?> 
<input type="submit" value="Save"/> 
</form> 

<?php 
if(isset($_GET['id'])) { 
    if(isset($_GET['artistname'])) { 
    $userinput = $_GET['artistname']; 
    echo "$userinput $artistID"; 
    $sqltwo = ("UPDATE artist SET artName='$userinput' WHERE artID='$artistID'"); 
    $stmt = $conn->prepare($sqltwo); 
    $stmt->execute(); 
    } else { 
     echo "this is broken 2"; 
    } 
    } 
?> 

这不是重复的,它没有询问第二个错误。主要问题是为什么用户输入不被读取?

+0

你必须在提交时传递id = action =“editartist.php?id = <?php $ _GET ['id']?>”'和那个'$ bound'变量是什么? – Chinito

+0

什么形式发送该ID?你可以发布吗? – Vbudo

+0

他正在发送邮件请求,并听取GET请求它不会以这种方式工作:) –

回答

0

因为你使用POST方法在HTML表单, 而PHP是等待GET方法

我重写代码假设你在用GET请求 如下发送ID

<?php 
if(isset($_GET['id'])) 
{ 
    $artistID = fix_string($_GET['id']); 
    $sql = ("SELECT artName FROM artist WHERE artID = '$artistID' "); 
    $result = $conn->prepare($sql); 
    $result->execute(); 
    $result->bind_result($bound); 
    $result->fetch(); 
} 
else { 
    echo "this is broken"; 
} 
?> 
<form method="post" action="editartist.php"/> 
<?php echo '<input type="text" name="artistname" value= "'.$bound.'">' ?> 
<input type="submit" value="Save"/> 
</form> 

<?php 
//no need to check for the id here , assuming you are posting data now.and you //already checked at the top of script for $_GET['id'] 
    if(isset($_POST['artistname'])) { 
    $userinput = $_POST['artistname']; 
    echo "$userinput $artistID"; 
    $sqltwo = ("UPDATE artist SET artName='$userinput' WHERE artID='$artistID'"); 
    $stmt = $conn->prepare($sqltwo); 
    $stmt->execute(); 
    } else { 
     echo "this is broken 2"; 
    } 

?> 

你需要考虑消毒Sanitize filters

,妥善结合PARAMS :)

+0

这不起作用,将消毒过滤器或绑定参数修复我的问题问题,还是只是我应该做的 – Chunelle

+0

再试一次我有错别字 –

+0

是啊,这仍然没有工作 – Chunelle