I cant insert in mysql from php - php

whats the solution for this problem Number of elements in type definition string doesn't match number of bind variables
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO register(un,pass,fn,ln,age,sex,email) VALUES (?,?,?,?,?,?,?)");
bind_param("sss",$regValue1,
$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
I have this code in php , i am trying to insert data to mysql but i face this problem Number of elements in type definition string doesn't match number of bind variables

Your type string in your statement doesn't have enough type specifiers in it.
bind_param("sss",$regValue1,$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
says that you have type "sss" which only corresponds to 3 of the 7 variables you specified. You need to add types for the rest.
From the documentation:
var1
The number of variables and length of string types must match the parameters in the statement.

The commands you are using are for using PDO to connect to an sql database and you are using mysqli. I gave an example using mysqli below that should work. The other option would be to change the connection to PDO type rather than mysqli.
<?php
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
$username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO register(un,pass,fn,ln,age,sex,email)".
"VALUES (`$regValue1`,`$regValue2`,`$regValue3`,`$regValue4`,`$regValue5`,`$regValue6`,`$regValue7`)";
$conn->query($sql);
echo $conn->affected_rows. " new records created successfully";
$conn->close();
?>

Related

MySQL table value be equal with php var (then display it)

I have the following MySQL table and I would like to do that RVR11 (500) be equal with a php var $RVR11
I have this code but it doesnt work.
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$price = mysql_query("SELECT RVR11 FROM tablename");
$result = mysql_fetch_array($RVR11);
echo $result['RVR11'];
Sorry about this beginner question.
You should choose a database before any queries, you can pass the fourth arguments to new mysqli constructor.

How to display number of rows in a sql table using php?

I'm trying to get lines' number from the result of the sql query, so I started by connecting to the database, then checking if the connection was established and finally I tried to count the number of lines, here's the code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$res = $conn->query('SELECT COUNT (id_offre) AS nb FROM offres');
$data = $res->fetch();
$nb = $data['nb'];
echo $nb;
?>
I get these errors :
Uncaught Error: Call to a member function fetch() on boolean
Call to a member function fetch() on boolean
I used echo to check the 'nb' value, what is the problem?
Your query fails which makes $conn->query() to return false.
So, why does the query fail?
SQL doesn't allow for any spaces between function names and the parentheses, like you have at COUNT ().
So if you change:
SELECT COUNT (id_offre) AS nb FROM offres
to
SELECT COUNT(id_offre) AS nb FROM offres
...it should work.
Note: I would highly recommend you to have a read about mysqli::error() and add some error handling to your code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
$res = $conn->query('SELECT * FROM offres');
$data_count = mysqli_num_rows($res);
echo($data_count);
?>
check this out!

Mysqli to variable in PHP

I've been a few days working on my project, but just stay stuck a specific part.
My problem is that i like to get a value from the database (mysqli)
But i always receive a 0. So, no value.
This is my code:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_idSQL = "SELECT user_id FROM ss_devices WHERE serialkey = '$serialkey'";
$user_idResult = $conn->query($user_idSQL);
$user_id = $user_idResult;
The script should see what the userid, it is in equality with the specified serial number.
But when i take a serial number like: FJRI433. I always get a 0. But in the database has this serial number user_id: 3.
I hope someone can help me out whith this problem.
Thanks.
You are executing the query but not fetching any results.
The manual has plenty of examples of using mysqli :
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
//Do stuff with the next row.
}
}
Try this perhaps...
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ( mysqli_connect_errno() ) {
die("Connection failed: " . mysqli_connect_error() );
}
$user_idSQL = "SELECT `user_id` FROM `ss_devices` WHERE `serialkey` = '$serialkey'";
if( $user_idResult = mysqli_query( $conn, $user_idSQL ) ) {
while ($row = mysqli_fetch_assoc($user_idResult)) {
echo $row['user_id'].'<br />';
}
}
mysqli_close( $conn );

mysql will not insert past fourth row

I'm not exactly sure what happened but this database and the php effecting it were working just fine until it hit the fourth row and now it won't insert new records at all.
if($_POST)
{
$servername = ******;
$username = ******;
$password = ******;
$db = ******;
$conn = mysqli_connect($servername, $username, $password, $db);
mysqli_select_db($conn,$db);
$uuid = $_POST['uuid'];
$sql = "INSERT INTO uuid VALUES ('$uuid');";
mysqli_query($conn,$sql);
mysqli_close($conn);
}
I'm not sure what happened but this is the relevant code for the mysqli query.
try this
<?php
if(isset($_POST['uuid']))
{
$servername = yourServerName;
$username = username;
$password = password;
$dbname = databaseName;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uuid = $_POST['uuid'];
$sql = "INSERT INTO tableName (columnName) VALUES ('$uuid')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Also, I recommend using prepared statements.

Get number from mysql into php varible

I have one number in a mysql database
This is my code:
$servername = "xxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT date FROM test";
$result1 = $conn->query($sql1);
$res1 = mysqli_fetch_assoc($result1);
echo $res1['date'];
I want to load that number into variable and count with that variable. After that i will store in same the db and table on the same place
$numvar = $res1['date'];
There yah go.

Categories