iT邦幫忙

0

PHP資料新增到資料庫的問題

這是我的程式碼的寫法,要將資料新增的phpmyadmin
確定有連上資料庫了,可是他一直說我未定義
因為要經由網頁輸入新增到資料庫

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//資料庫設定
//資料庫位置
$db_server = "127.0.0.1";
//資料庫名稱
$db_name = "my_db";
//資料庫管理者帳號
$db_user = "root";
//資料庫管理者密碼
$db_passwd = "0000";

//對資料庫連線
if(!@mysql_connect($db_server, $db_user, $db_passwd))
        die("can not connect");
if(!@mysql_select_db($con,"$db"))
{
echo"ok";
}
else
{
echo"faile";
}
//資料庫連線採UTF8
mysql_query("SET NAMES utf8");

//選擇資料庫
if(!@mysql_select_db($db_name))
        die("can not use");
	
$conn = mysql_connect($db_server, $db_user, $db_passwd) or die('Error with MySQL connection');
  
  mysql_query("SET NAMES 'utf8'");
  mysql_select_db($db_name);   
  
  $sql_insert = "INSERT INTO productmanage( productnumber, productname, finishDay, productAcount, badproduct,badproductAcount,reason) VALUES ('$productnumber','$productname','$finishDay','$productAcount','$badproduct','$badproductAcount','$reason')";	
	
 $productnumber=$_REQUEST["productnumber"];
 $productname=$_REQUEST["productname"];
 $finishDay=$_REQUEST["finishDay"];
 $productAcount=$_REQUEST["productAcount"];
 $badproduct=$_REQUEST["badproduct"];
 $badproductAcount=$_REQUEST["badproductAcount"];
 $reason=$_REQUEST["reason"];
 
 
?> 
![http://ithelp.ithome.com.tw/upload/images/20170110/20104281o5v2Cny0I1.png](http://ithelp.ithome.com.tw/upload/images/20170110/20104281o5v2Cny0I1.png)![http://ithelp.ithome.com.tw/upload/images/20170110/20104281slceCkkfPC.png](http://ithelp.ithome.com.tw/upload/images/20170110/20104281slceCkkfPC.png)

下面則是我的網頁程式碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,900" rel="stylesheet" />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<link href="fonts.css" rel="stylesheet" type="text/css" media="all" />
<style>
.relative1 {
  position: relative;
  top: -20px;
  left: 20px;
  background-color: white;
  width: 500px;
}
input {padding:5px 15px; background:#ccc; border:0 none;
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px; }

fieldset {
   border:100;
   padding:10px;
   margin-bottom:10px;
   
}




</style>
</head>
<body>

<div id="header-wrapper">
  <div id="header" class="container">
    <div class="relative1"><img src="123.jpg"></div>
    <div id="menu">
      <ul>
        <li class="current_page_item"><a href="index.html" accesskey="1" title="">首頁</a></li>
      </ul>
    </div>
  </div>
</div>


<div id="wrapper">
	
  <div id="featured-wrapper">
    <fieldset> 
    <form action="productinsert.php" method="post">
   製令編號:<input type="text" name="	productnumber"><br><br>
   產品名稱:<input type="text" name="productname"><br><br>
	完成時間:<input type="date" name="finishDay"><br><br>
	產品數量:<input type="int" name="productAcount"><br><br>	
	不良數:<input type="int" name="badproduct"><br><br>
	不良率:<input type="int" name="badproductAcount"><br><br>
    不良原因:<input type="text" name="reason"><br><br>
 <input type="submit" value="送出">
    </form>
	</fieldset>
  </div>

</div>

<div id="copyright" class="container">
  <p>© Copyright 2011-2015 </p>
</div>
</body>
</html>
看更多先前的討論...收起先前的討論...
fillano iT邦超人 1 級 ‧ 2017-01-10 18:07:13 檢舉
先把程式執行的順序一行一行搞清楚。你的程式是貼一貼湊起來的嗎?問題很明顯。
我只告訴你一件事。
不現在改學改用PDO,你將來就等著死路一條。
jerry00218 iT邦好手 10 級 ‧ 2017-01-11 00:17:06 檢舉
死路一條誇張了…
這可一點都不誇張……
最近就有人在問說他家的系統本來都是用mssql指令集……
結果「不小心」升上php7……
於是他自幹了mssql指令集……
我怎麼想這都不是正確的做法。
正確的做法是:你就乖乖的重寫整個sql到pdo吧!
乖乖加班吧。
所以其實看到mysql()指令集,我都希望大家「不要再用mysql()」來回答。
要嘛至少mysqli,最好是用pdo回答。強迫他們得學起來。
fillano iT邦超人 1 級 ‧ 2017-01-11 11:14:11 檢舉
我是都會寫一個抽象層隔離底下的php db extension...這樣需要變動時只要抽換底下的實作就好XD
@費公 懂的人會像你那樣做。但原本不熟的人遇到這問題就只能廠廠……
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2017-01-10 20:18:24

以下這個去改

<?
if( $_POST )
{
  $con = mysql_connect("localhost","inmoti6_myuser","mypassword");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("inmoti6_mysite", $con);

  $users_name = $_POST['name'];
  $users_email = $_POST['email'];
  $users_website = $_POST['website'];
  $users_comment = $_POST['comment'];

  $users_name = mysql_real_escape_string($users_name);
  $users_email = mysql_real_escape_string($users_email);
  $users_website = mysql_real_escape_string($users_website);
  $users_comment = mysql_real_escape_string($users_comment);

  $articleid = $_GET['id'];
  if( ! is_numeric($articleid) )
    die('invalid article id');

  $query = "
  INSERT INTO `inmoti6_mysite`.`comments` (`id`, `name`, `email`, `website`,
        `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
        '$users_email', '$users_website', '$users_comment',
        CURRENT_TIMESTAMP, '$articleid');";

  mysql_query($query);

  echo "<h2>Thank you for your Comment!</h2>";

  mysql_close($con);
}
?>

網友建議不要再用舊寫法
用以下這個PDO 的版本去改

<?php
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['comment'];

$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
  
try {
    $conn = new PDO("mysql:host=localhost;dbname=inmoti6_mysite","inmoti6_myuser","mypassword");
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$sql = "INSERT INTO `inmoti6_mysite`.`comments` (`id`, `name`, `email`, `website`,
        `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
        '$users_email', '$users_website', '$users_comment',
        CURRENT_TIMESTAMP, '$articleid');";    
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?> 

不過我是懷疑
樓主會在乎嗎

我覺得不要管他在乎不在乎。
現在的確剛好進入了php的mysql()廢止陣痛期
雖然的確有些聲音說,那萬一維護到舊系統怎麼辦?
那也該是遇到了,再來處理就好了。
我們不主動的來把舊的東西強迫革新的話。
就很難完全的迎來全新的世界。

如果她是我Google到的那位
現在該是資管系大三的學生
究竟是學生的認真學習
還是學期末要應付專題作業
讓我們繼續看下去

我要發表回答

立即登入回答