简体   繁体   English

将 mysql 表中的 php 记录插入到不同的表中

[英]Insert php record from mysql table into a different table

I've been searching for a long time for a solution to what I feel is a very simple problem.我一直在寻找解决我认为是一个非常简单的问题的方法。

I have a dynamically created page with a video that has a unique id.我有一个动态创建的页面,其中包含一个具有唯一 ID 的视频。 I also have a form that a user can submit content with.我还有一个用户可以提交内容的表单。 I want the id of the video to be included in the submission to tableA.我希望将视频的 id 包含在提交给 tableA 的内容中。

This code works great only when $id = 1.此代码仅在 $id = 1 时才有效。

$vidq = "SELECT * FROM tutorials";
$vidresult = mysql_query($vidq);
$vidrow = mysql_fetch_array($vidresult);

//form submission
if($_POST['formname'] == "submit") {

$name = $_POST['name'];
$id = $vidrow['id'];
$errorMessage = "";
if(empty($name)) {
    $errorMessage .= "<li>Please enter a valid name</li>";
    }

if(empty($errorMessage)) {

    $insert = "INSERT INTO tableA (videoid, name) VALUES (".$id.", ".$name.")"; 

    mysql_query($insert);
    exit();
    }
}

When I change $id to = 1, it posts, but when $id to = $vidrow['id'] it doesn't post.当我将 $id 更改为 = 1 时,它会发布,但当 $id 更改为 = $vidrow['id'] 时,它不会发布。

What am I doing wrong?我究竟做错了什么?

Try displaying the mysql error message by using mysql_errno/mysql_error.尝试使用 mysql_errno/mysql_error 显示 mysql 错误消息。 Eg...例如...

if (!mysql_query($insert))
{
    die('MySQL Fail (' . mysql_errno() . ') - ' . mysql_error());
}

mysql_errno() documentation - http://php.net/manual/en/function.mysql-errno.php mysql_errno() 文档 - http://php.net/manual/en/function.mysql-errno.php

  1. Have you tried to print out the contents of $id after $id = $vidrow['id'];你有没有试过在$id = $vidrow['id'];之后打印出$id的内容? ? ? It might reveal why it doesn't work the way you want...它可能会揭示为什么它不能按你想要的方式工作......

  2. Have you thought about what might happen if a malicious (or just curious) user calls your script with ?name=%27%27%29%3b%20DROP%20TABLE%20tableA%3B ?您是否想过如果恶意(或只是好奇)用户使用?name=%27%27%29%3b%20DROP%20TABLE%20tableA%3B调用您的脚本会发生什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM