I created a simple form through with I would like to update some info in mysql database. I through it all looks fine but i get some error
I got (two files):
<?php
$db_name = "test2";
$un = "jharvard";
$pw = "crimson";
$host = "localhost";
$connect = mysql_connect($host, $un, $pw);
mysql_select_db($db_name) or die(mysql_error());
echo ("succesfully conneted to the database!");
?>
and
<?php
if (isset($_POST['submitted']))
{
//Dit is de php file waarmee je connectie met de database maakt.
include ("addEmployee.php");
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO customers (name, address) VALUES ('$fname', '$lname')";
if (!mysqli_query($connect, $sqlinsert))
{
die ('error!');
}
}
?>
<html>
Add somebody!
<body>
<form method= "post" action="oefInsertData.php">
<input type = "hidden" name ="submitted" value="true" />
First name: <input type = "text" name="fname"/>
Last name: <input type = "text" name="lname"/>
<input type="submit" value="Add new person"/>
</form>
</body>
If i run it I get the following error: ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/jharvard/vhosts/pset7/public/oefInsertData.php on line 16
Anybody know what goes wrong here?
You are using both my_sql and my_sqli extension together..
Use this..
$connect = mysqli_connect($host, $un, $pw, $db_name);
and avoid
mysql_select_db($db_name) or die(mysql_error());
replace
<?php
$db_name = "test2";
$un = "jharvard";
$pw = "crimson";
$host = "localhost";
$connect = mysql_connect($host, $un, $pw);
mysql_select_db($db_name) or die(mysql_error());
echo ("succesfully conneted to the database!");
?>
with
<?php
$con=mysqli_connect("localhost","jharvard","crimson","test2");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
you have switch from mysql_ to mysqli_ so $connect is not a mysqli parameter change this by using mysqli_connect reference
<?php
$db_name = "test2";
$un = "jharvard";
$pw = "crimson";
$host = "localhost";
?>
and
<?php
if (isset($_POST['submitted']))
{
//Dit is de php file waarmee je connectie met de database maakt.
include ("addEmployee.php");
mysql_connect("$host", "$un", "$pw")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$query = mysql_query("INSERT INTO customers (name, address) VALUES ('{$fname}', '{$lname}')");
if(!$query){ echo mysql_error(); }
}
?>
Related
My program doesn't insert into the database, tried various means, I'm new to php and tried to test myself with this but I'm finding it difficult to get. i think the problem is at the connection to the database but I'm not getting it
<!DOCTYPE html>
<html>
<head>
<title>LIST</title>
</head>
<body>
<h1> TODO LIST </h1>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
try {
//create a database conneection
$conn = mysqli_connect("localhost", "root", "");
if(!$conn){
die("Database connection failed: ". mysql_error());
}
$sql = "INSERT INTO tasks (task, date, time) VALUES (:task, :date, :time)";
$query = $conn->prepare($sql);
$query->execute(array(':task'=>$task,
':date'=>$date,':time'=>$time));
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
?>
<form action = "todolist.php" method = "post">
Task: <input type="text" name="task" id="task"><br/>
Date: <input type="date" name="date" id="date"><br/>
Time: <input type="time" name="time" id="time"><br/>
<input type = "submit" value = "submit" name="submit">
</form>
</body>
</html>
You are not select any database.
Try something like this.
$conn = mysqli_connect("localhost", "root", "", "Yourdatabasename");
Are you connected to your database? You do not select any bdd and the logs are the right ones?
Maybe try:
$conn = mysqli_connect("localhost", "root", "", "databasename");
At first, see if you are connected to your database
I am using this way in one of my project. Create a connect.php file and create connection like this:
<?php
$conn = mysqli_connect("localhost", "root", "", "Yourdatabasename");
?>
Now in index.php file check if the submit button is pressed:
<?php
if(isset($_POST["submit"]))
{
// include connection file
include("connect.php");
// get values
$subject = mysqli_real_escape_string($con, $_POST["subject"]);
$comment = mysqli_real_escape_string($con, $_POST["comment"]);
// Insert in database
$query = "INSERT INTO comments(comment_subject, comment_text)VALUES ('$subject', '$comment')";
mysqli_query($con, $query);
}
?>
I am trying to create a register/login html/php script. I created a database and I believe my html/php code is correct. I was wondering if I am missing something small. Here is my code so far.
Here is the database
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
if(isset($_POST["submit"]))
{
$User = "**";
$Password = "**";
$Database = "member";
$Host = "localhost";
$con = mysqli_connect($Host, $User, $Password);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,$Database);
$myusername = $_POST["username"];
$mypassword = $_POST["password"];
$sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
mysqli_query($con,$sql);
mysqli_commit($con);
mysqli_close($con);
echo "Thank You! Information entered.";
}
else
{
?>
<form method="post" action="proInput.php">
Username:<input type="Text" name="username"><br>
Password:<input type="Text" name="password"><br>
<input type="Submit" name="submit" value="Register"></form>
<?
}
?>
Every time I type SELECT * FROM member; in MySQL my database is empty.
mysqli_commit($con);
mysqli_query($con,$sql);
mysqli_close($con);
You're committing before you insert, and then closing an uncommitted transaction. Try swapping the first two lines in the above excerpt.
As you answered already in your comment,
the name of the file isn't main_login.php but proinput.php
Your form is posting it's data to main_login.php and I'm assuming you don't have the insert query on that page, your code isn't run at all.
Options:
Try changing the name of this php file to main_login.php and then instead of the echo redirect the user to the login page
Move this part of the insert to your main_login.php
if(isset($_POST["submit"]))
{
$User = "";
$Password = "";
$Database = "member";
$Host = "localhost";
$con = mysqli_connect($Host, $User, $Password);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,$Database);
$myusername = $_POST["username"];
$mypassword = $_POST["password"];
$sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
mysqli_query($con,$sql);
mysqli_commit($con);
mysqli_close($con);
echo "Thank You! Information entered.";
}
else
{
?>
for debugging add the following to the very top of your php file right after the opening of php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
This worked for me
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
if(isset($_POST["submit"]))
{
$User = "db_user";
$Password = "db_password";
$Database = "db_name";
$Host = "db_host";
$con = mysqli_connect($Host, $User, $Password);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,$Database);
$myusername = $_POST["username"];
$mypassword = $_POST["password"];
$sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
mysqli_query($con,$sql);
mysqli_commit($con);
mysqli_close($con);
echo "Thank You! Information entered.";
}
else
{
?>
<form method="post" action="proinput.php">
Username:<input type="Text" name="username"><br>
Password:<input type="Text" name="password"><br>
<input type="Submit" name="submit" value="Register"></form>
<?php
}
?>
please add 'or die (mysqli_error())' at the end of your query and get the sql error report. That should tell you what is exactly wrong with sql . Php error reporting will only give php errors.
I'm a noob at PHP/MySQL. I've been looking around a lot but I can't figure out what's going wrong. Purpose of the script: update the value of item 1 and item 2 in the database of user with userID entered in txtUser.
I get my script working when I use a "fixed" value near userId in my query. However, when I try to use a variable ($player), it doesn't work. Seems like my variable is empty...
HTML:
<body>
<form id="form1" action="http://www.something.com/TestScript1.php" method="post" enctype="application/x-www-form-urlencoded">
<div>
<button type="submit" id="submit" value="Submit" title="SAVE">SAVE</button>
</div>
<div>
<input id="txtUser" name="txtUser" type="text" />
<input id="txtItem1" name="txtItem1" type="text" />
<input id="txtItem2" name="txtItem2" type="text" />
</div>
</form>
</body>
PHP :
$host = "localhost";
$user = "username";
$password = "password";
$database = "database";
$player = mysqli_real_escape_string($connection,$_POST['txtUser']);
$connection = mysqli_connect($host,$user,$password,$database) or die ("connection to server failed");
mysqli_select_db($connection,$database) or die ("couldn’t select database");
$item1 = mysqli_real_escape_string($connection,$_POST['txtItem1']);
$item2 = mysqli_real_escape_string($connection,$_POST['txtItem2']);
$query = "UPDATE table SET item1=$item1, item2=$item2 WHERE userId=$player";
$result = mysqli_query($connection,$query)
or die ("couldn’t execute update query: ".mysqli_error($connection));
echo "<h4>Data saved in the database</h4>";
mysqli_close($connection);
?>
Please put this line
$player = mysqli_real_escape_string($connection,$_POST['txtUser']);
After $connection
$connection = mysqli_connect($host,$user,$password,$database) or die ("connection to server failed");
Like
$connection = mysqli_connect($host,$user,$password,$database) or die ("connection to server failed");
$player = mysqli_real_escape_string($connection,$_POST['txtUser']);
Because you using $connection variable in mysqli_real_escape_string even before its initialised
$connection is not defined for line
$player = mysqli_real_escape_string($connection,$_POST['txtUser']);
please move this line after you create $connection
$connection = mysqli_connect($host,$user,$password,$database) or die ("connection to server failed");
<?php
$host = "localhost";
$user = "username";
$password = "password";
$database = "database";
$conn = mysqli_connect($host,$user,$password,$database) or die ("connection to server failed");
mysqli_select_db($conn,$database) or die ("couldn't select database");
/* The $conn object must be declared before first / any calls to mysqli_real_escape_string */
$player = mysqli_real_escape_string($conn,$_POST['txtUser']);
$item1 = mysqli_real_escape_string($conn,$_POST['txtItem1']);
$item2 = mysqli_real_escape_string($conn,$_POST['txtItem2']);
/* Values in query should be encapsulated in quotes if they are strings.. are they? */
$query = "UPDATE `table` SET `item1`='$item1', `item2`='$item2' WHERE userId='$player';";
/* Try not to reveal too many details in the event of an error, reduce the attack surface if possible! */
$result = mysqli_query($conn,$query) or die ("update failed: ");
echo "<h4>Data saved in the database</h4>";
mysqli_close($conn);
?>
I am created a small quiz with some functionality and i've ran into a hiccup. I am trying to write into a answers table with information gathered from 2 different tables.
My Question would be is there a way to insert UserId, QuestionId and the answer from the code i have below or can anyone help me with the code I have made.
Sorry if im abit of a noob. Code Below !
So far retrieve username from session
<?php
session_start();
//check if the user is already logged in.
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
?>
Then retrieve the QuestionId and userId and submit a answer
<section id="content" class="content">
<form action="testingq.php" method="post">
<p>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT QuestionId, QuestionText, InputTypeId FROM question";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> QuestionId: ". $row["QuestionId"]. " - : ". $row["QuestionText"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$host="localhost"; // Host name
$user="root"; // Mysql username
$pass="root"; // Mysql password
$db_name="mydb"; // Database name
// Connect to server and select databse.
$con = mysqli_connect($host, $user, $pass, $db_name);
$sql = "SELECT UserId FROM user WHERE username='$username'";
$result = mysqli_query($con, $sql);
?>
<input type="Radio" name="Answer" value="1" checked/>Yes</p>
<input type="Radio" name="Answer" value="0"/>No</p>
<p><input type="submit" value="Send it!"></p>
</form>
</section>
testingq.php
<!DOCTYPE html>
<html>
<body>
<?php
$host="localhost"; // Host name
$user="root"; // Mysql username
$pass="root"; // Mysql password
$db_name="mydb"; // Database name
// Connect to server and select databse.
$con = mysqli_connect($host, $user, $pass, $db_name);
$sql = "INSERT into answers (QuestionId, UserId, Answer) VALUES(
'$_POST[QuestionId]',
'$_POST[UserId]',
'$_POST[Answer]')";
$result = mysqli_query($con, $sql);
?>
</body>
</html>
hi i have an sql error says: no database selected this is my KK.php file:
$user_name = "root";
$password = "";
$database = "MyDB";
$server = "localhost";
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$message = mysqli_real_escape_string($link, $_POST['input1']);
$sql="INSERT INTO demo (message)
VALUES ('$message')";
if (!mysqli_query($link,$sql)) {
die('Error: ' . mysqli_error($link));
}
echo "1 record added";
mysqli_close($link);
?>
my database is "MyDB", and my demo.php file:
<form action="KK.php" method="post" />
<p>Message: <input type="text" name="input1" /></p>
<input type="submit" value="Submit" />
</form>
so what is the problem?, how do i select database?
Are you forgetting to call the mysqli_connect to get your connection?
ie:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Further to my comment above here is the code you are missing.
$user_name = "root";
$password = "";
$database = "MyDB";
$server = "localhost";
// This
$link = mysqli_connect($server,$user_name,$password,$database); // This
// This
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
You didn't call mysqli_connect
$con = mysqli_connect("localhost","my_user","my_password","my_db");