【问题标题】:cannot insert any string into mysql无法将任何字符串插入mysql
【发布时间】:2023-03-09 06:03:01
【问题描述】:

我在将字符串插入 mysql 时遇到问题。我可以插入数字但不能插入字符串。有人可以看看这个吗?感谢您的帮助。

我的表(剧集)看起来像

id  int(11) notnull          
serie   int(11) notnull          
name    varchar(255)    notnull          
comment varchar(255)    notnull          
embed   varchar(255)    notnull          

这里是 PHP 代码:

<?php
require_once "config.php";

$connect = mysql_connect( "$db_host", "$db_user", "$db_pw") or die(mysql_error());  
mysql_select_db("$db_name") or die( mysql_error() );

if(isset($_POST['pridatdiel'])) {   
    mysql_query("INSERT INTO `episodes` (`id`, `serie`, `name`, `comment`, `embed`)
    VALUES ('', '$cisloserie', '$menodielu', '$popis', '$embed')");
    header("refresh: 0;");
}

function stripinput($text) {
    if (!is_array($text)) {
        $text = stripslash(trim($text));
        $text = preg_replace("/(&amp;)+(?=\#([0-9]{2,3});)/i", "&", $text);
        $search = array("&", "\"", "'", "\\", '\"', "\'", "<", ">", "&nbsp;");
        $replace = array("&amp;", "&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;", " ");
        $text = str_replace($search, $replace, $text);
    } else {
        foreach ($text as $key => $value) {
            $text[$key] = stripinput($value);
        }
    }
    return $text;
}

if(isset($_POST['menodielu'])) {$menodielu = stripinput($_POST['menodielu']);}
if(isset($_POST['cisloserie'])) {$cisloserie = stripinput($_POST['cisloserie']);}
if(isset($_POST['popis'])) {$popis = stripinput($_POST['popis']);}
if(isset($_POST['embed'])) {$embed = stripinput($_POST['embed']);}

echo '    
  <form method="post">    
  meno dielu<input type="text" name="menodielu">       
  <select name="cisloserie">'; 
  $data = mysql_query("SELECT * FROM `series`") or die(mysql_error());   
  while($info = mysql_fetch_array( $data )) 
  {
  echo "<option value=".$info['serie_id'].">".$info['serie_id']."</option>";
  }
  echo '
  </select>     
  popis<input type="text" name="popis">        
  embed<input type="text" name="embed">      
  <input type="submit" value="pridať diel" name="pridatdiel">
  </form>
 ';
?>

【问题讨论】:

  • 您不检查错误。如果您不这样做,您如何期望解决这个问题?使用mysql_error() 获取 MySQL 报告的错误。
  • 如果你使用准备好的语句,你已经完成了。而且您不必先进行所有输入修改。

标签: php mysql string insert


【解决方案1】:

你正在执行你的查询:

if(isset($_POST['pridatdiel'])) {   
    mysql_query("INSERT INTO `episodes` (`id`, `serie`, `name`, `comment`, `embed`)
    VALUES ('', '$cisloserie', '$menodielu', '$popis', '$embed')");
    header("refresh: 0;");
}

...在初始化变量之前:

if(isset($_POST['menodielu'])) {$menodielu = stripinput($_POST['menodielu']);}
if(isset($_POST['cisloserie'])) {$cisloserie = stripinput($_POST['cisloserie']);}
if(isset($_POST['popis'])) {$popis = stripinput($_POST['popis']);}
if(isset($_POST['embed'])) {$embed = stripinput($_POST['embed']);}

MySQL 看到一个为每个字段插入一个空值的查询。您应该调高 PHP 的错误报告级别,以便在您尝试使用未定义变量时发出警告:

error_reporting(-1);

请使用 PHP 提供的 mysql_real_escape_string() 函数,而不是您的 stripinput()。展望未来,您还应该完全避免使用已弃用的 mysql 扩展 - 使用 PDOmysqli 两者都支持准备好的语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 2010-10-19
    • 2016-06-30
    • 1970-01-01
    • 2018-05-04
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多