开发者

Insert php record from mysql table into a different table

开发者 https://www.devze.com 2023-03-28 19:52 出处:网络
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. I also have a form that a user can su

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. I also have a form that a user can submit content with. I want the id of the video to be included in the su开发者_JAVA百科bmission to tableA.

This code works great only when $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.

What am I doing wrong?


Try displaying the mysql error message by using mysql_errno/mysql_error. Eg...

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

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


  1. Have you tried to print out the contents of $id after $id = $vidrow['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?

0

精彩评论

暂无评论...
验证码 换一张
取 消