【问题标题】:Trying to use an array value as an SQL insert value尝试使用数组值作为 SQL 插入值
【发布时间】:2016-06-11 03:26:03
【问题描述】:

嘿,伙计们,我正在尝试在新查询中使用从先前查询中获得的值,该值是存储在数组中的字符串,它们是 $Name 和 $Email 变量,当我var_dump them... string 'nathgold' (length=8) .... 我想用那个 nathgold 作为插入新查询的值。我收到错误Notice: Array to string conversion in C:\wamp\www\login\post.php on line 30

<?php
include_once('connect-db.php');
session_start();

if(!isset($_SESSION['isLogged']))
{
 header("Location: home.php");
 die();
}

    if (!isset($_REQUEST['MBID'])) exit;
    if (!isset($_REQUEST['Parent'])) {
        $Parent = 0;
    } else {
        $Parent = $_REQUEST['Parent'];
    }

    if (isset($_POST['Title'])) {

        $user_info=mysqli_query($connection, "SELECT * FROM usertest WHERE id=".$_SESSION['user']);
        $userRow=mysqli_fetch_array($user_info);

        $Name = $userRow=['username'];
        $Email = $userRow=['email'];
        $Title = mysqli_real_escape_string($connection, $_POST['Title']);
        $Message = mysqli_real_escape_string($connection, $_POST['Message']);
        $CurrentTime = time();
        // other filtering here...

        $result = mysqli_query($connection, "INSERT INTO mbmsgs (MBID, Parent, Poster, Email, Title, Message, DateSubmitted) VALUES ({$_REQUEST['MBID']}, $Parent, ".$Name.", ".$Email.", '$Title', '$Message', $CurrentTime);");
        if ($result) {
            echo "Your message has been posted - thanks!<br /><br />";
            echo "<A HREF=\"mbindex.php?MBID={$_REQUEST['MBID']}\">Back to messageboard</a>";
            exit;
        } else {
            echo "There was a problem with your post - please try again.<br /><br />";
        }
    }
?>     
<form method="post" action="post.php">

Message title: <input type"text" name="Title" /><br /><br />
Message:<BR />
<textarea name="Message" rows="10" cols="40"></textarea><br /><br />
<input type="hidden" name="MBID" value="<?php echo $_REQUEST['MBID']; ?>" />
<input type="hidden" name="Parent" value="<?php echo $Parent; ?>" />
<input type="submit" value="Post" />
</form>

【问题讨论】:

  • 您的语法有误。 $Name = $userRow=['username']; 应该是 $Name = $userRow ['username']; $Email... 相同,您现在所做的基本上是将 $userRow 设置为其中包含单词“username”的数组,然后将 $Name 设置为同一个数组。

标签: php mysql sql arrays string


【解决方案1】:

你要在字符串上转换 $name :

use implode("|",$name);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2013-07-11
    • 2016-07-29
    • 2014-08-10
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多