空格后不允许有字符。使用 PHP 插入 MySQL

发布于 2024-10-11 16:11:34 字数 2716 浏览 0 评论 0原文

好吧,我认为这很简单,但我不知道(我是 PHP 和 MySQL 的新手)。

我有一个从数据库中的表中获取数据的选择。我只是简单地采用用户选择的任何选项并将其放入带有 php mysql insert 语句的单独表中。

但我有一个问题。当我点击提交时,所有内容都会正确提交,除了任何带有空格的选择选项在第一个空格之后不会提交。例如,如果选项是COMPUTER REPAIR,则发送的所有内容都是COMPUTER。如果需要,我将发布代码,任何帮助将不胜感激。谢谢!

好的,这是我的选择代码:

<?php
  include("./config.php");
  $query="SELECT id,name FROM category_names ORDER BY name";
  $result = mysql_query ($query);
  echo"<div style='overflow:auto;width:100%'><label>Categories (Pick three that describe your business)</label><br/><select name='select1'><option value='0'>Please Select A Category</option>";
  // printing the list box select command
  while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt


  echo "<option>$catinfo[name]</option><br/>

 ";

  }

echo"</select></div>";

  ?>

这是我的插入代码(只是为了让您知道它包含所有内容,而不仅仅是选择!)

?php
    require("./config.php");
    $companyname = mysql_real_escape_string(addslashes(trim($_REQUEST['name'])));
    $phone = mysql_real_escape_string(addslashes($_REQUEST['phone']));
    $zipcode = mysql_real_escape_string(addslashes($_REQUEST['zipcode']));
    $city = mysql_real_escape_string(addslashes($_REQUEST['city']));
    $description = mysql_real_escape_string(addslashes($_REQUEST['description']));
    $website = mysql_real_escape_string(addslashes($_REQUEST['website']));
    $address = mysql_real_escape_string(addslashes($_REQUEST['address']));
    $other = mysql_real_escape_string(addslashes($_REQUEST['other']));
    $payment = mysql_real_escape_string(addslashes($_REQUEST['payment']));
    $products = mysql_real_escape_string(addslashes($_REQUEST['products']));
    $email = mysql_real_escape_string(addslashes($_REQUEST['email']));
    $select1 = mysql_real_escape_string(addslashes($_REQUEST['select1']));
    $select2 = mysql_real_escape_string(addslashes($_REQUEST['select2']));
    $select3 = mysql_real_escape_string(addslashes($_REQUEST['select3']));
    $save=$_POST['save'];
    if(!empty($save)){

    $sql="INSERT INTO gj (name, phone, city, zipcode, description, dateadded, website, address1, other2, payment_options, Products, email,cat1,cat2,cat3)
    VALUES
    ('$companyname','$phone','$city','$zipcode','$description',curdate(),'$website','$address','$other','$payment','$products','$email','$select1','$select2','$select3')";

    if (!mysql_query($sql,$link))
      {
      die('Error: ' . mysql_error());
      }
    echo "<br/><h2><font color='green' style='font-size:15px'>1 business         added</font></h2>";

    mysql_close($link);
}

    ?>

Ok so I think this is easy but I dont know (I'm a novice to PHP and MySQL).

I have a select that is getting data from a table in the database. I am simply taking whatever options the user selects and putting it into a separate table with a php mysql insert statement.

But I am having a problem. When I hit submit, everything is submitted properly except for any select options that have spaces don't submit after the first space. For example if the option was COMPUTER REPAIR, all that would get sent is COMPUTER. I will post code if needed, and any help would be greatly appreciated. Thanks!

Ok here is my select code:

<?php
  include("./config.php");
  $query="SELECT id,name FROM category_names ORDER BY name";
  $result = mysql_query ($query);
  echo"<div style='overflow:auto;width:100%'><label>Categories (Pick three that describe your business)</label><br/><select name='select1'><option value='0'>Please Select A Category</option>";
  // printing the list box select command
  while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt


  echo "<option>$catinfo[name]</option><br/>

 ";

  }

echo"</select></div>";

  ?>

And here is my insert code ( Just to let you know its got everything not just the select!)

?php
    require("./config.php");
    $companyname = mysql_real_escape_string(addslashes(trim($_REQUEST['name'])));
    $phone = mysql_real_escape_string(addslashes($_REQUEST['phone']));
    $zipcode = mysql_real_escape_string(addslashes($_REQUEST['zipcode']));
    $city = mysql_real_escape_string(addslashes($_REQUEST['city']));
    $description = mysql_real_escape_string(addslashes($_REQUEST['description']));
    $website = mysql_real_escape_string(addslashes($_REQUEST['website']));
    $address = mysql_real_escape_string(addslashes($_REQUEST['address']));
    $other = mysql_real_escape_string(addslashes($_REQUEST['other']));
    $payment = mysql_real_escape_string(addslashes($_REQUEST['payment']));
    $products = mysql_real_escape_string(addslashes($_REQUEST['products']));
    $email = mysql_real_escape_string(addslashes($_REQUEST['email']));
    $select1 = mysql_real_escape_string(addslashes($_REQUEST['select1']));
    $select2 = mysql_real_escape_string(addslashes($_REQUEST['select2']));
    $select3 = mysql_real_escape_string(addslashes($_REQUEST['select3']));
    $save=$_POST['save'];
    if(!empty($save)){

    $sql="INSERT INTO gj (name, phone, city, zipcode, description, dateadded, website, address1, other2, payment_options, Products, email,cat1,cat2,cat3)
    VALUES
    ('$companyname','$phone','$city','$zipcode','$description',curdate(),'$website','$address','$other','$payment','$products','$email','$select1','$select2','$select3')";

    if (!mysql_query($sql,$link))
      {
      die('Error: ' . mysql_error());
      }
    echo "<br/><h2><font color='green' style='font-size:15px'>1 business         added</font></h2>";

    mysql_close($link);
}

    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乜一 2024-10-18 16:11:34

value="$catinfo[name]" 放在 标记上,使它们看起来像这样:

echo "<option value=\"".htmlspecialchars($catinfo['name'])."\">".$catinfo['name']."</option>";

由于您省略了它们,因此它使用中的任何内容默认情况下,在 标记之间,并且由于它们周围没有引号,因此它将空格确定为值的末尾。通过将要插入数据库的实际值放在 value 属性内,您可以清楚地告诉它使用引号之间的完整短语。

另外,请删除
,因为您不需要在

Place the value="$catinfo[name]" on your <option> tags so they look like this:

echo "<option value=\"".htmlspecialchars($catinfo['name'])."\">".$catinfo['name']."</option>";

Since you've omitted them, it's using whatever is in between the <option> tags by default and since there is no quotes around them, it determines the space as being the end of the value. By placing the actual value you want inserted into the database within the value attribute, you're distinctly telling it to use the full phrase between the quotes.

Also, remove the <br/> as you don't need to place breaks within a <select> element.

甩你一脸翔 2024-10-18 16:11:34

这是人们经常遇到的错误。为了按原样访问数据库中存储的数据,请遵循以下格式:

echo "<option>'".$catinfo[name]."'</option><br/>";

如果您希望将值存储在选项中,请使用以下格式:

echo "<option value='".$catinfo[name]."'>'".$catinfo[name]."'</option><br/>";

它将按原样显示数据库中的数据。
我希望它有帮助!

It's a common mistake that people encounter. In order to access the data stored in Database as it is Kindly follow the following format:

echo "<option>'".$catinfo[name]."'</option><br/>";

if you want value to stored in option use the following format:

echo "<option value='".$catinfo[name]."'>'".$catinfo[name]."'</option><br/>";

It will display data from database as it is.
I hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文