Edited: This is the error I got.
$categoryName = $_POST['category_name'] ;
$categoryDesc = $_POST['category_desc'] ;
$sql = "INSERT INTO category (category_title, category_desc) VALUES ('$categoryName','$categoryDesc')";
if (mysqli_query($con,$sql))
{
echo 'Inserted successfully';
}
else
{
echo 'Inserted Failed';
}
mysqli_close($con);
?>
The error I got is Fatal error: Function name must be a string in line 12
line 12 : $categoryName = $_POST['category_name'] ;
[EDITED]
dbconnect.php (I am not sure that I am right or wrong because I am using virtual host, that's why my servername is my virtual host name)
<?php
$servername = "wp-one";
$username = "root";
$password = "";
$dbName = "personality_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
html code
<div class="col-lg-6">
<form role="form" action="../controller/AddCategory&Question.php?create=true" method="post">
<div class="form-group">
<label>Category Name</label>
<input class="form-control" type="text" placeholder="CategoryName" name="category_name" />
</div>
<div class="form-group">
<label>Category Description</label>
<input class="form-control" type="text" placeholder="CategoryDesc" name="category_desc" />
</div>
<div class="form-group input_fields_wrap">
<button class="add_field_button btn btn-default" style="margin-bottom:10px;">Add New Question</button>
<div class="form-group">
<div class="row">
<div class="col-lg-2"><input type="text" placeholder="Number" class="form-control" name="criteria[]"></div>
<div class="col-lg-5"><input type="text" placeholder="Question" class="form-control" name="grade[]"></div>
</div>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
Please help me, Thank You so much
$conn = new mysqli(...);
so you must use $conn instead of $con
if (mysqli_query($conn,$sql))
{
echo 'Inserted successfully';
}
else
{
echo 'Inserted Failed';
}
mysqli_close($conn);
whats your data type for category_title in database?
Your Error in connection
// Create connection
$conn = new mysqli($servername, $username, $password, $dbName);
you use $conn and in insert file you use $can instead of $conn...
Related
I've been trying to figure this out for hours and it seems like there are multiple ways of doing it but for some reason I can't seem to get it to work correctly. For some reason my table is being updated and I am only seeing new rows with a new auto increment integer but the remaining columns are left blank. There is a bit more to that form but I left it off to keep this as short as possible. Thanks for the help!
File: dbh.inc.php
$dbServername = "localhost";
$dbUsername = "username";
$dbPassword = "password";
$dnName = "database_name";
$conn = mysqli_connect($dbServername, $dbUsername,
$dbPassword, $dnName);
if(!$conn)
// creation of the connection object failed
die("connection object not created: ".mysqli_error($conn));
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
File with form:
$name7 = $_POST['name7'];
$email7 = $_POST['email7'];
$phone7 = $_POST['phone7'];
$message7 = $_POST['message7'];
$sql = "INSERT INTO user_contacts (name7, email7, phone7, message7) VALUES ('".$_POST["name7"]."','".$_POST["email7"]."','".$_POST["phone7"]."','".$_POST["message7"]."')";
mysqli_query($conn, $sql);
?>
<div class="form-group">
<form action="dbh.inc.php" method="POST">
<input type="text" class="form-control" name="name7" id="name7" placeholder="<?php esc_html_e('Name:','listingpro'); ?>">
<span id="name7"></span>
</div>
<div class="form-group form-group-icon">
<i class="fa fa-envelope" aria-hidden="true"></i>
<input type="email" class="form-control" name="email7" id="email7" placeholder="<?php esc_html_e('Email:','listingpro'); ?>">
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone7" id="phone7" placeholder="<?php esc_html_e('Phone','listingpro'); ?>">
<span id="phone7"></span>
</div>
<div class="form-group">
<textarea class="form-control" rows="5" name="message7" id="message7" placeholder="<?php esc_html_e('Message:','listingpro'); ?>"></textarea>
</div>
I have Just Edited your sql a little bit. Try It
$sql = "INSERT INTO user_contacts (name7, email7, phone7, message7) VALUES ('".
$name7. "','" . $email7 . "','". $phone7 ."','". $message ."')";
There is something wrong with my code :
On the edit page, I want to show the user the previous value in the input box.
But one error I keep getting about the value is the following :
Notice: Undefined variable: gebruikers_naam in C:\xampp\htdocs\website_herkansing\edit_gebruiker.php on line 72
I think there is something wrong with the isset/submit part but I just
can not figure it out..
Here is the code I'm working with
<?php
session_start();
define('DB_NAME', 'ochtendgloren');
$servername = "localhost";
$username = "root";
$password = "";
$db = "ochtendgloren";
$tbl = "members";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submit']))
{
$gebruikers_naam = mysqli_real_escape_string($db, $_POST['gebruikers_naam']);
htmlentities($gebruikers_naam);
$id = $_GET['id'];
$query = "UPDATE members
SET gebruikers_naam = '$gebruikers_naam'
WHERE id = '$id' " ;
$result = $conn->query($query);
if($result){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('edit succesvol!')
window.location.href='admin_members.php';
</SCRIPT>");
}
}
?>
<html>
<head>
<link rel="stylesheet" href="boekingsform.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abel">
</head>
<div class="boeken">
<h1>Wijzig hier de gebruiker</h1>
<form action="editrij.php?id=<?= $id ?>" method="post" >
<div class="row">
<div class="col-25">
<label for="gebruikers_naam"> vul hier de nieuwe gebruikers naam in: </label>
</div></div>
<br>
<div class="row">
<div class="col-75">
<input type="text" name="voornaam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>
</div>
</div>
<br>
<input type="submit" value="submit" name="submit" />
</form>
</div>
</html>
I think the problem is related with your variable usage and name of input box it must be gebruikers_naam
On line :
<input type="text" name="voornaam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>
You may use $gebruikers_naam only to print the name.
Also you must assign a global variable before before using it in if clause.
Just assign null like $gebruikers_naam = ""; after variables declarations.
You aren't sending the variable gebruikers_naam, but voornaam. This line of code
<input type="text" name="voornaam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>
Should be
<input type="text" name="gebruikers_naam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>
Also, because you are using the variables $id and $gebruikers_naam in the form, you should assign them a value before the if clause.
Still not completely sure the 'flow' of your code makes a lot of sense, but following should help a bit:
<?php
session_start();
//define('DB_NAME', 'ochtendgloren'); // not used in your code, commented out
$servername = "localhost";
$username = "root";
$password = "";
$db = "ochtendgloren";
$tbl = "members";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submit']))
{
if(isset($_POST['gebruikers_naam']) && !empty($_POST['gebruikers_naam'])) { // added condition: we need a 'gebruikers_naam'
$gebruikers_naam = mysqli_real_escape_string($conn, $_POST['gebruikers_naam']); // changed $db to $conn
// the return value (encoded string) of htmlentities is not used in your code
// so commented it out
//htmlentities($gebruikers_naam);
$id = $_GET['id'];
$query = "UPDATE members
SET gebruikers_naam = '$gebruikers_naam'
WHERE id = '$id' " ;
$result = $conn->query($query);
}
if($result){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('edit succesvol!')
window.location.href='admin_members.php';
</SCRIPT>");
} else { // added else {} statement
echo "<script> alert('Error: could not update the database'); </script>"; // added: error message
}
}
?>
<html>
<head>
<link rel="stylesheet" href="boekingsform.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abel">
</head>
<div class="boeken">
<h1>Wijzig hier de gebruiker</h1>
<form action="editrij.php?id=<?php echo $id; ?>" method="post" > <!-- you need to echo $id and close with ';' - changed: echo $id; -->
<div class="row">
<div class="col-25">
<label for="gebruikers_naam"> vul hier de nieuwe gebruikers naam in: </label>
</div></div>
<br>
<div class="row">
<div class="col-75">
<?php $gebruikers_naam = (isset($gebruikers_naam)) ? $gebruikers_naam : 'N/A'; ?> <!-- added a test to see if $gebruikers_naam is available -->
<input type="text" name="voornaam" required="required" value="<?php echo $gebruikers_naam; ?>"/> <!-- variable is $gebruikers_naam, changed (echo and ';') -->
</div>
</div>
<br>
<input type="submit" value="submit" name="submit" />
</form>
</div>
</html>
I'm trying a simple upload of datas from a table in a database with mysqli functions and sql.
I don't know what i'm doing of wrong.
This is the HTML code:
<form name="formCompany" method="post" action="AddCompany.php" >
<div class="modal-body">
<div class="container w-75">
<input type="text" id="nameCompany" name="nameCompany"
class="form-control" placeholder="Name" aria-label="Name" aria-
describedby="basic-addon1">
<br/>
<input type="url" id="webCompany" name="webCompany"
min="1900" class="form-control" placeholder="WebSite" aria-label="WebSite"
aria-describedby="basic-addon1">
<br/>
<input type="text" id="placeCompany" name="placeCompany"
class="form-control" placeholder="Place" aria-label="Place" aria-
describedby="basic-addon1">
<br/>
<input type="email" id="emailCompany" name="emailCompany" class="form-control" placeholder="Email" aria-label="Email" aria-describedby="basic-addon1">
<br/>
<input type="text" id="noteCompany" name="noteCompany"
class="form-control" placeholder="Note" aria-label="Note" aria-
describedby="basic-addon1">
</div>
</div>
<div class="modal-footer">
<button type="submit" id="submitCompany" class="btn btn-
primary">Save</button>
</div>
</form>
and this the page "AddCompany.php" :
<?php
include 'DB.php';
$name = $_POST["nameCompany"];
$place = $_POST["placeCompany"];
$web = $_POST["webCompany"];
$email = $_POST["emailCompany"];
$note = $_POST["noteCompany"];
// Inserisce una nuova compagnia
$sql2="SELECT company_id FROM company ORDER BY company_id DESC LIMIT 1;";
$result=mysqli_query($connessione,$sql2);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
$last_id=$row[0];
$id = $last_id+1;
$sql ="insert into company (company_id, name, place, web,email,note) values
('$id','$name','$place','$web','$email','$note');";
$res1=mysqli_query($connessione, $sql);
if($res1 !=FALSE) {
header("Location: Principale.php");
}
else
{
echo "Impossibile aggiungere Compagnia<br>";
echo "<a href='Principale.php'>Clicca Qui</a> per tornare alla pagina
principale.";
}
?>
It always print me the else because it finds $res1=FALSE
Why?
Try this one, you will see what you are doing wrong
DB.php
define ("DB_USER", "Database username");
define ("DB_PASSWORD", "Database password");
define ("DB_DATABASE", "Database name");
define ("DB_HOST", "Hostname");
//# $con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
# $con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if($con->connect_error)
die("Unable to connect to database : ".$con->connect_error);
Change the query executions
$res1=$con->query($sql);
if($res1){
header("Location: Principale.php");
}
else
{
echo "Error -> ".mysqli_error($con);
echo "Impossibile aggiungere Compagnia<br>";
echo "<a href='Principale.php'>Clicca Qui</a> per tornare alla pagina principale.";
}
name is keyword in Mysql you have to us `
your query must to be something like this
$sql ="insert into company (company_id, `name`, place, web,email,note) values
('$id','$name','$place','$web','$email','$note');";
I am using WAMP to try and learn a little PHP and SQL. I'm trying to take user input from a very basic table here:
<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e-mail" name="e-mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="Password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" />
</div>
</fieldset>
</form>
and I want to be able to take the input and post to a database. I've been trying to make this happen with this code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ($_POST[name], $_POST[e-mail], $_POST[password])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
var_dump('name', 'e-mail', 'password');
?>
When I try and insert the "" as in $_POST["name"] I get an error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16
When I try to remove the "" I get this error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16
I also tried to set the variables in the top of the code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$name=$_POST['name']
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ('name','email', 'password');
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
var_dump('name', 'e-mail', 'password');
?>
This way I ended up with an error message saying:
( ! ) Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in C:\wamp\www\input.php on line 9
I was able to echo the name in another script using the $_POST, I am not sure why it will not work with the SQL command. If anyone would help out, and/or give me some resources to learn/study from as well I would appreciate it!
You have a missing ';' after:
$name=$_POST['name']
Hi Please note some points
1) You will get warnings of undefined index because you are not checking about values posted or not. We Should use isset() before using values.
2) Second try to use lowercase whenever you are giving any name to any tag in php to be sure that no error exist due to typing in lowercase or uppercase.
3) Try to use underscore if your word is long so use e_mail;
So you can use this php code
<?php
if(isset($_POST['register'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$email = $_POST['e_mail'];
$password = $_POST['password'];
$sql = "INSERT INTO MyGuests(name, email, password) VALUES('$name','$email','$password')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
}
?>
I have added name to your submit button also changed e-mail to e_mail and name="Password" to name="password"
<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e_mail" name="e_mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" name="register" />
</div>
</fieldset>
</form>
Im new in MYSQL & PHP so don't mind me about my stolen code XD
I want to publish simple posts by using PHP/HTML & saving on Database.
Ayway here is the file that i've created to post stuff on home.php
admin.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dblogin";
$newsTitle = null;
$newsShortDescription = null;
$newsFullContent = null;
$newsColor = null;
$newsIcon = null;
$users_website = null;
$users_comment = null;
if(isset($_POST['btn-post']))
{
$newsTitle = $_POST['news_title'];
$newsShortDescription = $_POST['news_short_description'];
$newsFullContent = $_POST['news_full_content'];
$newsColor = $_POST['news_color'];
$newsIcon = $_POST['news_author'];
}
$newsTitle = mysql_real_escape_string($newsTitle);
$newsShortDescription = mysql_real_escape_string($newsShortDescription);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO info_news (news_title, news_short_description, news_full_content, news_author, news_color)
VALUES ('$newsTitle', '$newsShortDescription', '$newsFullContent', '$newsIcon', '$newsColor')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn = null;
?>
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="news_title" placeholder="Title" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_short_description" placeholder="Description" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_full_content" placeholder="Full Content" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_color" placeholder="Color" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_author" placeholder="Icon" required />
</div>
<div class="clearfix"></div>
<div class="form-group">
<button type="submit" name="btn-post" class="btn btn-block btn-primary">
Post
</button>
</div>
<br />
</form>
Thank you :)
I want to make kind of a simple blog, only Title, Short Description and so... It works while i make new posts, but when i refresh it shows an empty post. Here is a pic: Here is the Image also i've tried to edit the admin.php ( By looking other questions on SOF )
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dblogin";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("INSERT INTO info_news (news_title, news_short_description, news_full_content, news_author, news_color) VALUES (:news_title, :news_short_description, :news_full_content, :news_author, :news_color)");
$stmt->bindParam(':news_title', $news_title);
$stmt->bindParam(':news_short_description', $news_short_description);
$stmt->bindParam(':news_full_content', $news_full_content);
$stmt->bindParam(':news_author', $news_author);
$stmt->bindParam(':news_color', $news_color);
$stmt->execute($_POST);
?>
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="news_title" placeholder="Title" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_short_description" placeholder="Description" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_full_content" placeholder="Full Content" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_color" placeholder="Color" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_author" placeholder="Icon" required />
</div>
<div class="clearfix"></div>
<div class="form-group">
<button type="submit" name="btn-post" class="btn btn-block btn-primary">
Post
</button>
</div>
<br />
</form>
By looking some other questions on stackoverflow but still no luck ;/
it's not an empty post what makes the empty row inserted, try this:
if(isset($_POST['btn-post']))
{
$newsTitle = $_POST['news_title'];
$newsShortDescription = $_POST['news_short_description'];
$newsFullContent = $_POST['news_full_content'];
$newsColor = $_POST['news_color'];
$newsIcon = $_POST['news_author'];
$newsTitle = mysql_real_escape_string($newsTitle);
$newsShortDescription = mysql_real_escape_string($newsShortDescription);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO info_news (news_title, news_short_description, news_full_content, news_author, news_color)
VALUES ('$newsTitle', '$newsShortDescription', '$newsFullContent', '$newsIcon', '$newsColor')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn = null;
}
I moved the "}" after $conn = null; ;)
And BTW, you should redirect after each POST. So POST is not sent once more on page refresh.
And you can't redirect while the content was "echoed" already. It will work for a while thanks to buffer, but when the header will grow in its size (like 4kB, depends on server settings) it will fail one day. (saw it few times in github you posted)