Form isn't posting data into MySQL database? - php

Trying to get this form to add new data into my database yet for some reason it doesn't seem to work? Instead of staying on the same page "admin.php?page=3" it redirects to just "admin.php" and no echos or signs that it is even doing anything? Could someone help me out here? Cheers!
Form:
<form>
<form name="postnewstory" action="admin.php?page=3" method="POST">
<strong>News Title: </strong><input type="text" name="news_title"><br><br />
<strong>News Story:</strong><br>
<textarea name="news_body" rows="4" cols="60"></textarea><br><br />
<input type="file" name="news_photo"><br>
<strong>Story Link: </strong><br />
<input name="button" type="radio" value="0" checked="checked">No Link<br>
<input type="radio" name="button" value="1">Link<br><br />
<strong>Link Address: </strong><input type="text" name="news_link"><br>
<strong>News Story Tags: </strong><input type="text" name="news_tags">
<input type="submit" value="Post" name="postnewstory" class="btn btn-success"><br />
</form>
PHP:
I know its very basic, just trying to get it to work before I add error checks or anything.
<?php
if (isset($_POST['postnewstory'])){
$username = $user_data['username'];
$email_address = $user_data['email_address'];
$news_title = $_POST['news_title'];
$news_photo = $_POST['news_photo'];
$button = $_POST['button'];
$news_link = $_POST['news_link'];
$news_tags = $_POST['news_tags'];
$exists = mysql_query ("SELECT * FROM users WHERE username='$username'") or die ("not found");
if (mysql_num_rows($exists) != 0){
//update the info in database
mysql_query ("INSERT INTO news (`news_id` ,`news_title` ,`news_body` ,`news_photo` ,`news_date` ,`username` ,`news_tags` ,`button ,`news_link`)
VALUES (NULL , '$news_title', '$news_body', '$news_photo', now(), '$username', '$news_tags', '$button', '$news_link');") or die ("update didn't work");
echo "<div class='alert alert-success'><strong>This post was sent!</strong></span></div> ";
echo '<meta http-equiv="refresh" content="2;url=admin.php?page=3">';
} else echo "<strong><font color=red>Update did not work, please try again.</font></strong>";
}
?>

You have a missing backtick in:
,`button
change to:
,`button`
Rewrite:
("INSERT INTO news (`news_id` ,`news_title` ,`news_body` ,`news_photo` ,`news_date` ,`username` ,`news_tags` ,`button`,`news_link`)
Footnotes:
I quote Shankar:
"Replace die ("update didn't work"); with die (mysql_error()); to know the exact error why your query ain't working."

I think this should work;
Change the line
echo '<meta http-equiv="refresh" content="2;url=admin.php?page=3">';
to
echo "<script>window.location = 'admin.php?page=3';</script>";

Related

PHP - INSERT Query failing, returns no error. Beginner

<form action="" method="post">
<input type="text" id="title" name="title" />
<input type="text" id="link" name="link" />
<input type="submit" value="Add resource" />
<?php
if(isset($_POST['title']) && $_POST['link']) {
$t = $_POST['title'];
$l = $_POST['link'];
$con = mysqli_connect("localhost","root","","rman");
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL:" . mysqli_connect_error());
}
mysqli_query($con, "INSERT INTO tutorials (id, title, link, section) VALUES ('','$t','$l','')");
}
?>
</form>
How can this not work? I have removed every single part that might have caused this. Nothing is going in the database, no errors returning whatsoever.
For everyone wondering:
DB name: rman
Table name: tutorials
colums: id (INT11, Auto increment), title (Text), link (Text), section(INT11)
Am I being blind here? I'm sorry if thats the situation. Hope someone can see what I am doing wrong and help me out.
This should work.
<?php
if(isset($_POST['title']) && $_POST['link']) {
$t = $_POST['title'];
$l = $_POST['link'];
$con = mysqli_connect("localhost","root","","rman");
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL:" . mysqli_connect_error());
}
mysqli_query($con, "INSERT INTO tutorials (id, title, link, section) VALUES ('', '$t', '$l', '1')");
}
?>
<form action="" method="post">
<input type="text" id="title" name="title" />
<input type="text" id="link" name="link" />
<input type="submit" name="submit" value="Add resource" />
</form>
use mysqli_error() to check error code, as well as don't insert blank id if it's AI
Thusly:
if (!mysqli_query($con, "INSERT INTO tutorials (id, title, link, section) VALUES ('','$t','$l','')"))
{
echo mysqli_error($con);
}

Php form submission-Error

<?php
include ("db.php");
if (isset($_POST['register'])) {
echo $name = ($_POST["name"]);
echo $email = ($_POST["email"]);
echo $uname = ($_POST["uname"]);
echo $password = ($_POST["pass"]);
$result = mysql_query($con,'SELECT * from company_profile where user_name = "'.$uname.'" or email = "'.$email.'"');
if(mysql_num_rows($result) > 0){
echo "Username or email already exists.";
}else{
$query = mysql_query($con,"INSERT INTO company_profile(user_name, password, company_name, email, phone, country, activation_string) VALUES ('$uname','$password','$name','$email','','','')");
if($query){
echo "data are inserted succesfully.";
}else{
echo "failed to insert data.";
}
}
}
?>
HTML form
<form action="register.php" method="post" id="reg" onsubmit='return validate();'>
Company Name:
<input type="text" class="inputs" name="name" id="name" /><br />
Email:
<input type="text" class="inputs" name="email" id="txtEmail" /><br />
User name:
<input type="text" class="inputs" name="uname" id="uname"/><br />
Password:
<input type="password" class="inputs" name="pass" id="pass1"/><br />
Conferm Password:
<input type="password" class="inputs" name="cpass" id="pass2"/><br /><br />
<input type="submit" value="Register" class="button" />
</form>
Trying to check whether username or email exist in database if yes alert user if no insert values to db. The code doesn't work for me. no error no echo no insertion. any solution?
You have no field with name register
Regarding this
if (isset($_POST['register'])) {
It will never evaluate to true.
<input type="submit" value="Register" class="button" />
needs name attribute name="register"
In addition:
http://php.net/mysql_query
You have wrong order of arguments. However, using mysql_* is highly NOT encouraged. It is an obsolette database API with a lot of vulnerabilities. Switch to mysqli or PDO instead
your first check isset($_POST['register']) is always false because there is no input in your form with the name="register"
and also you chould fix your query , $con should be the second parametre.
and keep in mind that your code is highly vulnerable don't publish this on a server except of your localhost
Try this:-
$result = mysql_query('SELECT * from company_profile where user_name = "'.$uname.'" or email = "'.$email.'"',$con);
mysql_query
as you are using mysql_query, the link identifier should be second paramater
$result = mysql_query('SELECT * from company_profile where user_name = "'.$uname.'" or email = "'.$email.'"', $con);
You are over complicating yourself...
First of all, to check the user, just do this when form submits:
var_dump(mysql_num_rows(mysql_query("select bla bla where username=`{$_GET['username']}`")));
//by bla bla i mean primary key, don't select *, affects performance.
Also, please see this, you have an error in your syntax... take a look at the right parameters for mysql_query
http://ro1.php.net/mysql_query
After you set your queries and do the above test and figure out if its good or not, you can cut the 3-4 lines you have above in 1 if line like this
if(mysql_num_rows(mysql_query("select bla bla where username=`{$_GET['username']}`"))) { //code here
}
Try Without $con in INSERT query
$query = mysql_query("INSERT INTO company_profile
(
user_name, password,
company_name, email,
phone, country,
activation_string
)
VALUES
(
'$uname', '$password',
'$name', '$email',
'', '',
''
)");
You need to specify the name to your input field and check for that:-
<input type="submit" name="register" value="register">
OR
<button type="submit" name="register">Register</buton>

Multiple form sign up not working

I have a sign up process which involves two forms being submitted. The problem is with the first form not being submitted. Also, I need a way to relate the two forms as information from both is inserted into the same table row, I think this can be done by taking the previous table row id.
It is supposed to work like this: First a user must search for an item in the search bar. Matches are then displayed with radio buttons next to each one and a submit button at the bottom. When submitted, the form data (which is the result of the search that they checked with the radio) goes into the database table 'users'. The 'users' table contains a row for id, username, password and radio.
The radio option is submitted into radio. This also creates an id, which is auto incremented. That is the first form. Once the radio option is picked and the data is in a table row, the user must fill out the second form which asks for an email and a password, which is submitted into the same row that the radio option is in.
When I go through this process, the email (referred to as username in table) and password appear in the table along with the id, but the radio is always blank. Not sure why the radio option is not being submitted. Also not sure if i need a way to relate the forms. I am a beginner at this so, please try to make answers understandable. Thanks in advance, heres the code:
<?php
//This code runs if the form has been submitted
if (isset($_POST['submit'])) {
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
die('The email '.$_POST['username'].' is already in use.');
}
// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}
// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}
// now we insert it into the database
$insert = "INSERT INTO users (username, password, radio)
VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['radio']."')";
$add_member = mysql_query($insert);
?>
<h2><font color="red">Registered</font></h2>
<p>Thank you, you have registered - you may now login</a>.</p>
<?php
}
else
{
?>
<font color = #000000><h1>Sign Up</h1></font>
<?php
// Search Engine
// Only execute when button is pressed
if (isset($_POST['keyword'])) {
// Filter
$keyword = trim ($_POST['keyword']);
// Select statement
$search = "SELECT * FROM tbl_name WHERE cause_name LIKE '%$keyword%'";
// Display
$result = mysql_query($search) or die('That query returned no results');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
while($result_arr = mysql_fetch_array( $result ))
{
?>
// Radio button
<input type="radio" name="radio">
<?php
echo $result_arr['cause_name'];
echo " ";
echo "<br>";
}
?>
<input type="submit" name="radio_submit" value="Select Item">
</form>
<?php
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "We don't seem to have that cause. You may add a cause by filling out a short <a href='add.php'>form</a>.<br><br>";
}
}
?>
<!--Sign Up Form-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="sign_up_form">
<input type="text" name="keyword" placeholder="Search" onFocus="this.select();" onMouseUp="return false;">
<input type="submit" name="search" value="Search">
<br />
<br />
<input type="email" name="username" maxlength="250" placeholder="Your email" onFocus="this.select();" onMouseUp="return false;">
<input type="password" name="pass" maxlength="50" placeholder="Password" onFocus="this.select();" onMouseUp="return false;">
<input type="password" name="pass2" maxlength="50" placeholder="Re-type Password" onFocus="this.select();" onMouseUp="return false;">
<br />
<input type="submit" name="submit" value="Sign Up">
</form>
<?php
}
?>
Modify your code like this
<input type="radio" name="radio" value="<?php echo $result_arr['cause_name']; ?>" >
and then submit it you should get the value
update the below query as well
<?php
echo $insert = "INSERT INTO users (username, password, radio) VALUES ('$_POST[username]', '$_POST[pass]', '$_REQUEST[radio]')";
$add_member = mysql_query($insert);
?>
change the below code
//This code runs if the form has been submitted
if (isset($_POST['radio_submit'])) {
/////
<?php
if (isset($_REQUEST['submit']))
{
$radio = $_REQUEST['radio'];
$insert = mysql_query("INSERT INTO users (radio) VALUE ('$radio')");
if (!$insert)
{
echo mysql_error();
}
}
?>
<form action="" method="get">
<input type="radio" name="radio" value="red">red
<input type="radio" name="radio" value="blue">blue
<input type="submit" name="submit" />
</form>

MySql is not updating

please help..i give up..
trying to update MySql table..
the file is settings.php (self-submitting)
form code is:
<form method="POST" action="settings.php">
First Name <input type="text" name="fname" id="set_fname" />
Last name <input type="text" name="lname" id="set_lname" />
Email: <input type="text" name="email" id="set_email" />
mobile <input type="text" name="mobile" id="set_mobile" />
Bio: <textarea name="bio" id="set_bio" cols="" rows="5" maxlength="1000" ></textarea>
<input type="submit" id="personal_submit" value="Update" />
</form>
php part
$id=$_SESSION['id'];
$fname_update =$_POST['fname'];
$lnam_update = $_POST['lname'];
$email_update = $_POST['email'];
$mobile_update =$_POST['mobile'];
$bio_update =$_POST['bio'];
$db_host="localhost";
$db_uname="admin";
$db_pass="mypass";
$db_db="main_db";
$db_table="mebers_general";
mysql_connect("$db_host","$db_uname","$db_pass") or die (mysql_error());
mysql_select_db("$db_db") or die("no database by that name");
if($_POST['fname']){
$sql_personal_update=mysql_query("UPDATE members_general SET first_name='$fname_update' WHERE id='$id' ") or die (mysql_error()) ;
if($sql_personal_update){
echo "should be ok but no record is updated....Heeelppp";
}else{
echo "Smth is Wrong";
}
}
I get no errors..
as a result i get the echo string "should be ok but no record is updated....Heeelppp";
so it detects no problem in updating
I've tried the same query in phpMyadmin - works just fine.
I checked the variables - they are not empty..
everything seems to be as it should.
Please help..stacked for 2 days already here//
$sql_personal_update=mysql_query("UPDATE members_general SET firs_name='".$fname_update."' WHERE id='".$id."' ") or die (mysql_error()) ;
You are not actually RUNNING your query:
$sql_personal_update=("UPDATE members_general SET firs_name='$fname_update' WHERE id='$id' ") or die (mysql_error()) ;
^^^^^--- where's mysql_query()?
It should be
$sql_personal_update = mysql_query(blah blah blah) or die(mysql_error());
I guess it's because you're not actually calling the mysql_query function!
For starters, try
$sql_personal_update=mysql_query("UPDATE members_general SET firs_name='$fname_update' WHERE id='$id' ") or die (mysql_error()) ;
Also, is firs_name a typo, or did you really call your database column that?
EDIT: As JoachimIsaksson suggested, mysql_connect etc are deprecated. Use mysqli instead, like below:
$mysqli = new mysqli($db_host,$db_uname,$db_pass, $db_db) or die (mysql_error());
if($_POST['fname']){
$sql_personal_update=$mysqli->query("UPDATE members_general SET first_name='$fname_update' WHERE id='$id' ") or die (mysql_error()) ;
if($sql_personal_update) {
echo "should be ok but no record is updated....Heeelppp";
} else {
echo "Smth is Wrong";
}
}
Thanks for everything, guys, i just recoded all page from blank and it worked..have no idea what was the reason..maybe the 20th hour in a row of coding..thanx..

Input data Guestbook PHP

Im trying to create a guestbook with mysql database. I have no trouble display the sql data on my form.
But when im trying to input data my send button dosent work. I think the problem is in this code, but I cant find it. and have done the tutorial a couple of times.
I dont have any error messages. But this is some of the code.
thanks
<?php
if(isset($_GET['page'])){
echo "
<form action='guest_process.php' method='post'>
<p>Name: <input type='text' name='name'> </p>
<p>Email: <input type='text' name='email'> </p>
<p>Comment: </p>
<p><textarea name='comment'></textarea></p>
<hr />
<p><input type='button' name='submit' value='Post Entry'></p>
</form>
";
}else{
$connect = mysql_connect('localhost','root','') or die ('Couldnt connet');
$db = mysql_select_db('guestbook');
$query = mysql_query('select * from guestbook order by id desc');
$num_rows = mysql_num_rows($query);
if($num_rows > 0){
//display entries
while($row = mysql_fetch_assoc($query)){
echo "
<p>
<b>Name: </b>".$row['name']."
</p>
<p>
<b>Email: </b>".$row['email']."
</p>
<p>
<b>Comment: </b>".$row['comment']."
</p>
<p>
<b>Date: </b>".$row['date']." | Time: ".$row['time']."
</p>
<hr />
";
}
} else{
echo 'no entries in database';
}
}
?>
The guestbook is seperated in two php files.
This is the other page the guest_process.php
<?php
if($_post['submit']){
$connect = mysql_connect('localhost','root','') or die ('Couldnt connet');
$db = mysql_select_db('guestbook');
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$comment = n12br($_POST['comment']);
$date = date('Y-m-d');
$time = date('H:i:s');
$query = mysql_query("insert into guestbook values('','$name','$email','$comment','$date','$time')");
header('Location: index.php');
}else {
header ('Location: index.php');
}
?>
use <input type="submit" value="Post Entry"> instead of <input type=button>
this is not good:
<input type =button name='submit' value='Post Entry'>
it should be:
<input type="submit" name="submit" value="Post Entry">
The problem is that there is no code to put anything in a database.
First you need to actually post something. You might think that this line does this, but it doesn't:
<input type =button name='submit' value='Post Entry'>
Apart from the fact you need " around the type, a "button" isn't actually something that does submitting. You'll need client-side code for that. You could better change it to
<input type="button" name='submit' value='Post Entry'>
You won'nt be there though. Now you need to take the info from the $_POST variable (try var_dump($_POST) to see what's in there), and put it in your database. You can find the appropriate commands for SQL and the php-mysql connection in your tutorial probably

Categories