这是我的代码:
<?php
if (isset($_GET['ig'])) {
******$fromig=$_GET['ig']******
if ($fromig = 1) {
require 'pdo-instagram.php';
echo '<script>alert("Instagram");</script>'
}
} else if (isset($_GET['fb'])) {
******$fromfb=$_GET['ig']******
if ($fromfb = 1) {
require 'pdo-facebook.php';
echo '<script>alert("Facebook");</script>'
}
}
?>
基本上,我想在我的页面中添加一个功能,它会告诉我人们来自哪里。例如:
在我的 Instagram 个人资料中,我会将 http://joshmurray.eu/?ig=1
添加到我的个人资料中,以便 PHP 可以查看该字符串,并确定将信息放入哪个表中对于这种情况,它将INSERT
一个时间戳到表instagram
我在上面加了星号的部分,我知道那部分是错误的,但我不知何故必须使用 mysql_real_escape_string
的 PDO 版本,但我不知道如何使用。 (我想,我不太擅长 PHP)
非常感谢任何帮助。
<小时/>编辑:我没有选择正确的答案,因为没有一个有效但是这是我有效的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO from_instagram (timestamp)
VALUES (now())";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
我HAD将我的表名称从from-instagram
更改为from_instgram
,因为我忘记了PHP解释a -
作为减号,因此当执行代码时,看起来我正在从 instagram
中减去 from
。
感谢各位的帮助!
最佳答案
这是一个示例,您可以根据您的要求进行扩展。
<?php
if (isset($_GET['ig'])) {
if($_GET['ig'] == 1){
// Insert the data to table
$tableName = 'instagram';
// Or you may use your own external file
if( insertData($tableName) ){
// Data inserted
}
}
}
if (isset($_GET['fb'])) {
if($_GET['fb'] == 1){
// Insert the data to table
$facebook = 'facebook';
// Or you may use your own external file
if( insertData($facebook) ){
// Data inserted
}
}
}
function insertData($table){
// Database credentials
$hostname = 'your_host';
$username = 'your_username';
$password = '';
$database = 'your_database';
try {
// Connect to database
$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Now you are connected to database let'd do the operations
// Instead of mysql_real_escape_string you can use a prepared statement
$stmt = $dbh->prepare('INSERT INTO $table ( Time ) VALUES ( now() )');
$stmt->execute();
// If you are not using a MySQL function do this way
// $stmt = $dbh->prepare('INSERT INTO instagram ( Time ) VALUES ( :Time )');
// $stmt->execute(array(':Time' => $time )); // $time is the variable which contains the value
} catch (PDOException $e) {
return false;
}
}
?>
我希望这会有所帮助。
关于如果查询字符串等于值,php添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34435381/