What's new

Closed php pdo error

Status
Not open for further replies.

Pusoy000

Forum Veteran
Joined
Jan 30, 2017
Posts
1,645
Reaction
963
Points
735
Age
25
I need help mga master please :(

my Main.php
Code:
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="POST" action="code2.php">
    <input type="text" name="usn">
    <input type="text" name="fname" placeholder="first name">
    <input type="text" name="lname" placeholder="Last name">
    <input type="submit" name="view" value="SELECT">
    <input type="submit" name="insert" value="INSERT">
</form>
</body>
</html>

my code.php
PHP:
<?php
    include('includes/db_connection.php');
    $conn = getDB();
        function sql($conn, $q, $params, $return){
        //prepare statement
        $stmt = $conn->prepare($q);
        //execute statement
        $stmt->execute($params);
       
        //Decide whether to return the rows themselves , or just the rows
        if ($return == "rows") {
            return $stmt->fetchAll();
        }
        elseif ($return == "count") {
            return $stmt->rowCount();
        }
    }
?>

my code2.php
PHP:
<?php
    // Select / Call function
    include('code.php');
    $usn = $_POST['usn'];

    if (isset($_POST['view'])) {
        # code...
        $conn = getDB();
        $rows = sql($conn, "SELECT * FROM tbl_student WHERE usn = ?", array($usn), "rows");
        // Get results
        foreach ($rows as $row) {
            echo $row['usn'].' '.$row['fname'].' '.$row['lname'];
        }
    }
    //INSERT
    if (isset($_POST['insert'])) {
        # code...
        $lname = $_POST['lname'];
        $fname = $_POST['fname'];
        $conn = getDB();
        sql($conn, "INSERT INTO tbl_student('fname','lname') VALUES (?,?)", array($fname, $lname));
    }
   
    // //UPDATE
    // sql($conn, "UPDATE tbl_student SET fname = ? WHERE usn = ?", array($fname, $usn))

    // //DELETE
    // sql($conn, "DELETE FROM tbl_student WHERE usn = ?", array$usn);
?>


sample usn id: aa
OUPUT for retrieving data is ok :
571579


error in inserting data I dont know why huhu
input:
firstname : phcorner
lastname: phcorner
and got this error
571580


nag eexpect sya ng apat daw since okay naman sa retrieving data dito lang talaga ako may problema insert chuchu ano ba problema sa function ko

GOAL is to make a function that could execute all queries.
 

Attachments

yung sql() na nideclare mo may 4 na parameters... pero dun sa insert procedure mo... nagbigay ka lang ng tatlo... wala kang nasupply para sa $return parameter... kaya error sya...

redundant din yun getdb() sa mga query... andun na sya initially sa included php page e...
 
Last edited:
Code:
sql($conn, "INSERT INTO tbl_student('fname','lname') VALUES (?,?)", array($fname, $lname));

#Change it To

sql($conn, "INSERT INTO tbl_student('fname','lname') VALUES (?,?)", array($fname, $lname),"rows");
 
sa function na
function sql($conn, $q, $params, $return)
4 parameters.
tapos sa pag gamit mo
sql($conn, "INSERT INTO tbl_student('fname','lname') VALUES (?,?)", array($fname, $lname))
3 lang.
...
sana maka tulong... hehe
 
Status
Not open for further replies.

Similar threads

Back
Top