PHP Fatal Error with inserting stuff into database

Hello.

I’ve been trying for the past 10 hours or so trying to solve this PHP fatal error. I’ve looked all over Stack Overflow, and many more sites to find the solution. Unfortunately, I have not found the missing piece and I’m humbly asking for a hand.

The error is: **Fatal error** : Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\affc\index.php:48 Stack trace: #0 {main} thrown in **C:\xampp\htdocs\affc\index.php** on line **48**

My code is as follows.


                $conn=mysqli_connect("localhost","","",""); /* This is connected properly. For the sake of my database privacy, I am not disclosing it. */
                // Check connection
                if (mysqli_connect_errno()){
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    die();
                }

                if(isset($_POST['submit']))
                {		
                    $post_user = $_POST['content'];
                
                    $save = $conn->prepare("INSERT INTO psots (status_id, status, timestamp) VALUES ?,?,?");
                    $save->bind_param("sss",$post_user);
                    $save->execute();
                
                    if(!$insert)
                    {
                        echo mysqli_error();
                    }
                    else
                    {
                        echo "Records added successfully.";
                    }
                }```

SQL if needed:

SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `affc`
--

-- --------------------------------------------------------

--
-- Table structure for table `psots`
--

CREATE TABLE `psots` (
  `status_id` int(11) NOT NULL,
  `status` tinytext CHARACTER SET cp1250,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `psots`
--
ALTER TABLE `psots`
  ADD PRIMARY KEY (`status_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `psots`
--
ALTER TABLE `psots`
  MODIFY `status_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

This is wrong. Needs parentheses around the question marks.

1 Like
               $save->bind_param("sss",$post_user);

Unless $post_user is an array of 3 parameters (which it likely isnt), this will fail. You’re telling bind_param you’re going to send it 3 things, and then only giving it one.

I attempted to fix my code to fit @m_hutley and your solution but it still shows the same error. Here is the code:

                {		
                    $post_user = $_POST['content'];
                
                    $save = $conn->prepare("INSERT INTO psots (status_id, status, timestamp) VALUES '?,?,?'");
                    $save->bind_param("s",$post_user);
                    $save->execute();
                
                    if(!$insert)
                    {
                        echo mysqli_error();
                    }
                    else
                    {
                        echo "Records added successfully.";
                    }
                }```

I said parentheses. These aren’t parentheses. These are single quotation marks you added.

Fixed it but a parse error came up. Parse error : syntax error, unexpected ‘,’ in C:\xampp\htdocs\affc\index.php on line 47

This right here says what’s wrong. You probably have a comma hanging somewhere in the SQL query. Please post your updated code.

                {		
                    $post_user = $_POST['content'];
                
                    $save = $conn->prepare("INSERT INTO psots (status_id, status, timestamp) VALUES "?,?,?"");
                    $save->bind_param("s",$post_user);
                    $save->execute();
                
                    if(!$insert)
                    {
                        echo mysqli_error();
                    }
                    else
                    {
                        echo "Records added successfully.";
                    }
                }```

Again, these aren’t parentheses. These are double quotation marks. If you are having issues with understanding what a parentheses is, look at your current SQL query.

$save = $conn->prepare("INSERT INTO psots (status_id, status, timestamp) VALUES "?,?,?"");
                                          ^--------           ---------^

Those are parentheses.

Looks like I stroked out, but putting it in parentheses arises a new error:

Warning : mysqli_stmt::bind_param(): Number of variables doesn’t match number of parameters in prepared statement in C:\xamppest\htdocs\affc\index.php on line 48

                {		
                    $post_user = $_POST['content'];
                
                    $save = $conn->prepare("INSERT INTO psots (status_id, status, timestamp) VALUES (?,?,?)");
                    $save->bind_param("s",$post_user);
                    $save->execute();
                }```

Alright, so this error actually pertains to your original parameter numbers. They were correct, but you were just only passing in 1. You are expected to pass in the same amount of variables in bind_param as the same amount of data types. All of this should correlate with your actual SQL query. So you have to be passing in 3 things in your SQL query, in the data type option, and your variables.

#1: Unless you’ve done some very creative naming of MySQL tables, you’ve typoed the table name.
#2: If you’re going to put a timestamp on the entry, you’re probably using the default value of CURRENT_TIMESTAMP anyway, and can probably eliminate it from the query if that’s the case.
#3: If status_id is an INT, you’ll need to pass an int, not a string for that parameter.
#4: You need to pass three (or two, if you eliminated timestamp in #2) parameters, of the correct type.

2 Likes

Everything works now. Thanks so much!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.