使用PHP将下拉列表中的数据插入数据库 [英] Inserting Data from dropdown into database with PHP

查看:225
本文介绍了使用PHP将下拉列表中的数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我需要一个可以轻松更新的下拉列表,因此我创建了一个名为 我以表格形式列出要选择的制造商的制造商.

First I needed a dropdown list that I could update easily so I created a database called manufacturers where I list manufacturers to be selected in a form.

我最终用以下代码完成了此操作:

I finally accomplished this with this code:

<?php
 // Connect to the test datbase on localhost
 // That's where we created the countries table above
 mysql_connect('localhost','##user##','##pass##');  mysql_select_db('wordpress');

 // Query the countries table and load all of the records
 // into an array.
 $sql = 'select * FROM manufacturers';
 $res = mysql_query($sql) or die(mysql_error());
 while ($rec = mysql_fetch_assoc($res))
 $manufacturers[] = $rec;
 ?>
<form action="select.php" method="post">
<?php
 echo '<select name="dropdown">';
 foreach ($manufacturers as $c)
{
  if ($c['id'] == $_GET['id'])
   echo "<option value=\"{$c['meta_id']}\" selected=\"selected\">{$c['meta_value']}              </option>\n";
 else
  echo "<option value=\"{$c['meta_id']}\">{$c['meta_value']}</option>\n";
 }
echo '</select>';
?>
 <input type="submit" value="Submit" name="submit"/>
 </form>

这很好,我现在有一个下拉列表,该列表是从我的列表中填充的 数据库制造商.

This worked out great I now have a dropdown list that is populated from my database manufacturers.

现在,我需要将其发送到现有的数据库调用post_meta,以便从那里永久显示用户选择.

Now I need to send this to an existing database call post_meta so that from there I can display the users selection permanently.

我尝试了几种不同的选择,但是我试图使用以下代码将其发送到我的post_meta数据库.

I have tried a couple of different options but I am trying to use the following code to send this to my post_meta database.

<?php
$con = mysql_connect("localhost","##user##","##pass##");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("wordpress", $con);

$sql="INSERT INTO wp_postmeta (meta_id, post_id, meta_key, meta_value)
VALUES
('$_POST['meta_id']}','$_POST[post_id]','$_POST[meta_key]','$_POST[meta_value]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
?>

这实际上是插入数据库中,但不记录任何值.

This actually inserts into the database but doesn't record any values.

请帮助我弄清楚我在做错什么.

Please help me figure out what I'm doing wrong.

推荐答案

执行此操作的正确方法是A:逃避所有这些$ _POST超全局变量.
B.编写一个查询,如下所示.

The proper way to do this is to A: escape all those $_POST superglobals.
and B. Write a query as shown below.

这是wp_postmeta的tabledef:
http://codex.wordpress.org/Database_Description#Table:_wp_postmeta

Here's the tabledef for wp_postmeta:
http://codex.wordpress.org/Database_Description#Table:_wp_postmeta

因为meta_id是自动递增的主键,所以不提供它,而MySQL提供.

Because meta_id is an auto_increment primary key, you do not provide it, MySQL does.

//$meta_id = mysql_real_escape_string($_POST['meta_id']);  <<-- not needed.
$post_id = mysql_real_escape_string($_POST['post_id']);
$meta_key = mysql_real_escape_string($_POST['meta_key']);
$meta_value = mysql_real_escape_string($_POST['meta_value']);
$sql=" INSERT INTO wp_postmeta
       (post_id, meta_key, meta_value)
       VALUES
       ('$post_id','$meta_key','$meta_value') ";  //<<-- don't forget the quotes!
if ($result = mysql_query($sql)) {
  //You can get the new meta_id using:
   $new_meta_id = mysql_insert_id($result);
} else { 
   die ("could not insert ".mysql_error());
}

这篇关于使用PHP将下拉列表中的数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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