0
<?php
//connection to the database 
try {     
  $pdo = new PDO('mysql:host=localhost;dbname=frostedc_movies;charset=utf8',
                    frostedc_user, 'pass'); 
 echo "connected"; 
}  
catch(PDOException $e) {  
 echo $e; //2 
} 

// select everything from the news table
$query = "SELECT * FROM movie";// Table name NOT database name 

foreach ($pdo->query($query) as $row) {
    echo "<table border='1'>";
    echo "<tr>";
        echo "<td width='150'>".$row['movietitle']."</td>";
        echo "<td width='150'>".$row['genre']."</td>";
        echo "<td width='150'>".$row['LastViewed']."</td>";
        echo "<td width='150'>".$row['Location']."</td>";
    echo "</tr>";

}
echo "</tr>";
echo "</table>";
echo "<br>";
echo "
    <form>
        <p>Please Enter a Movie Title</p>
        <input type='text' name='new_movie' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Genre</p>
        <input type='text' name='movie_genre' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Last View Date</p>
        <input type='text' name='last_view' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Location</p>
        <input type='text' name='movie_loca' />
        <input type='submit' value='Submit' />
    </form>";




$pdo = null;

?>

这是新的更新代码。我正在尝试使用输入将数据输入到我的数据库中。我已经研究过如何做到这一点,但到目前为止我还没有任何工作。有什么想法吗?我是否更容易使用包含并在 html 中进行输入?如果是这样,我可以使用它们将数据输入数据库吗?

4

4 回答 4

1

你在这里做两件错误的事情

第一:PDO第二混合:你不能从数据库中选择。你需要从你的表中选择!(你确定是你的桌子吗?)MySQL
$query = "SELECT * FROM myDB"; myDB

于 2013-09-21T04:18:22.177 回答
0

当您标记您的问题PDO时,我已从您的示例中删除了所有不需要的代码。

<?php
//connection to the database 
try {     
    $pdo = new PDO('mysql:host=localhost;dbname=frostedc_movies;charset=utf8',
                    user, 'password'); //1
    echo "connected"; 
}  
catch(PDOException $e) {  
    echo $e; //2 
} 
// select everything from the news table
$query = "SELECT * FROM myTable";// Table name NOT database name 
echo "<table>";
echo "<tr>";
foreach ($pdo->query($query) as $row) {//3
    echo "<td>".$row['movietitle']."</td>";
    echo "<td>".$row['genre']."</td>";
    echo "<td>".$row['LastViewed']."</td>";
    echo "<td>".$row['Location']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
$pdo = null;//5
?>

评论数

1 设置字符集手册

2 仅用于开发的回显错误消息。在生产中,您应该对其进行处理或删除 try/catch。

3 由于没有参数使用query()

4 对于PHP 5.3.6之前的 utf8

5 将mysql_close();(mysql_) 更改为$pdo = null;(PDO)

于 2013-09-21T08:21:24.423 回答
-1
<?php // connect to the database
  $host = 'localhost';
  $username = 'user';
  $pass = 'password';
  $conn=mysql_connect($host,$username,$pass)or die(mysql_error());
  mysql_select_db("myDB");
  // select everything from the news table
  $query = "SELECT * FROM news";
  $result = mysql_query($query);

  while($row = mysql_fetch_array($result)){

    echo "<table>
            <tr>
              <td>".$row['movietitle']."</td>
              <td>".$row['genre']."</td>
              <td>".$row['LastViewed']."</td>
              <td>".$row['Location']."</td>
            </tr>
          </table>";
  }

  // disconnect from the database
  mysql_close();
?>
于 2013-09-21T04:39:48.250 回答
-1

试试这个代码

<?php // connect to the database
$host = 'localhost';
$username = 'user';
$pass = 'password';
mysql_connect($host,$username,$pass);
mysql_select_db("myDB");
// select everything from the news table
$query = "SELECT * FROM `tablename`";
$result = mysql_query($query);
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['movietitle']."</td>";
echo "<td>".$row['genre']."</td>";
echo "<td>".$row['LastViewed']."</td>";
echo "<td>".$row['Location']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
于 2013-09-21T08:57:51.953 回答