使用 PHP 对 Mysql 的动态输入错误 [英] Dynamic Input Error To Mysql Using PHP

查看:33
本文介绍了使用 PHP 对 Mysql 的动态输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是数据库

CREATE TABLE IF NOT EXISTS `student` (
  `id` int(11) NOT NULL,
`name` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `student`
ADD PRIMARY KEY (`id`);
ALTER TABLE `student`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

这是 HTML 部分:jsFiddle

Here is the HTML part: jsFiddle

这里是向 mysql 插入数据的 PHP 部分:

And Here is the PHP part inserting data to mysql:

<?php
$connect = mysql_connect('localhost','root','');
if (!$connect) {
   echo mysql_error();
}
$db = mysql_select_db('demo');
if (!$db) {
  echo mysql_error();
} 
$all_names ="";
if(isset($_POST["mytext"])){   
   foreach($_POST["mytext"] as $key => $text_field){
      $all_names .= $text_field .", ";
 }
}

$result = "INSERT INTO student ( name ) VALUES( $all_names )";
$insert_row  = mysql_query($result);
if(!$insert_row){
   echo $result;
}

向数据库插入值时出错:

Getting Error inserting value to database:

INSERT INTO student ( name ) VALUES( dffd, )

推荐答案

更改

   foreach($_POST["mytext"] as $key => $text_field){
      $all_names .= $text_field .", ";
   }

$all_names = "'" . implode("'), ('", $_POST["mytext"]) . "'";

这篇关于使用 PHP 对 Mysql 的动态输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆