如果查询字符串等于值,则php添加到数据库

时间:2015-12-23 12:08:18

标签: php mysql pdo

这是我的代码:

<?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可以查看字符串,并确定它将信息放入哪个表。对于这种情况,它将{{1表格INSERT

中的时间戳

我在上面提到的那个部分,我知道那部分是错的,但我不得不使用instagram的PDO版本,我不知道如何。 (我想,我不擅长PHP)

非常感谢任何帮助。

编辑:我没有选择正确的答案,因为没有一个正常的但是这是我的代码有效:

mysql_real_escape_string

HAD 将我的表名从<?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(); ?> 更改为from-instagram,因为我忘记了PHP将from_instgram作为一个减去符号所以当代码执行时,我似乎从-中减去from

感谢帮助人员!

2 个答案:

答案 0 :(得分:0)

以下是一个示例,您可以根据自己的要求进行扩展。

<?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;
    }
}
?>

我希望这会有所帮助。

答案 1 :(得分:0)

<?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>'
        }
}
?>

你必须使用&#34; ==&#34;在if条件下(等于)。

试试我的代码。