簡體   English   中英

將一個輸入的數據插入兩個mysql列

[英]Insert data from one input into two mysql columns

我有一個由Dreamweaver CS5創建的簡單的insert.php公式。 我希望能夠在此公式編輯器中的“新聞”標題中輸入到數據庫的“ news_headline”列中,然后將該標題自動添加到“ news_slug”列中,並將小寫字母和空格轉換為減號。

因此,如果我在表單的“標題”輸入字段中輸入“ This is a headline”,則會在“ news_headline”列中輸入“ This is a headline”,並將“ this-is-a-headline”添加到“ news_slug”列。

到目前為止,我得到了以下代碼:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$news_headline = $_POST['news_headline'];
$news_slug = str_replace(' ', '-', $news_headline);

  $insertSQL = sprintf("INSERT INTO tbl_news (news_headline, news_slug) VALUES (%s, %s)",
                   GetSQLValueString($_POST['news_headline'], "text"),
                   GetSQLValueString($_POST['news_slug'], "text"));

基本休息

    mysql_select_db($database_Jahrhundertkomet, $con);
    $Result1 = mysql_query($insertSQL, $Jahrhundertkomet) or die(mysql_error());}
    mysql_select_db($database_Jahrhundertkomet, $con);
    $query_Jahrhundertkomet = "SELECT * FROM tbl_news";
    $Jahrhundertkomet = mysql_query($query_Jahrhundertkomet, $con) or die(mysql_error());
    $row_Jahrhundertkomet = mysql_fetch_assoc($con);
    $totalRows_Jahrhundertkomet = mysql_num_rows($con);
    mysql_free_result($con);
    ?>
    <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
    <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">News_headline:</td>
      <td><input type="text" name="news_headline" value="" size="32"></td>
    </tr>
        <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Datensatz einfügen"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>

但是現在,當我在代碼中輸入標題時,會出現錯誤:“ news_slug”列不能為空。 要通過轉換將我的輸入從news_headline輸入到news_slug,我需要更改什么?

如果您只想用空格代替分數,則可以使用str_replace :您也可以使用$_POST['news_slug']; 這不會收到任何信息,因為您的表單中沒有名為news_slug的輸入。

嘗試這個:

 $news_headline = $_POST['news_headline'];
 $news_slug = str_replace(' ', '-', $news_headline);

並將這些行放在這一行之后:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM