SQL error when creating my own form in joomla - php

hey guys i want to creat my own form in joomla and use the post method .
so i download extension to make my own code in articles in joomla like this
<form action="http://localhost/ocr/includes/Create_Subject.php" method="post">
username <input type="text" name="menu_name" value=""/><br/>
password <input type="text" name="id" value=""/><br/>
<input type="submit" name="save" value="Submit" />
</form>
and then i go to my website data base and create a table called show to add into it my values which i get it from my form and also
make a php file called (Create_Subject.php) and i put it in includes file in my website and called it by action like u see in my code in my html and the code of that php here
<?php
$coonect=mysql_connect("localhost","root","");
if(!$coonect){
echo "Data_base error";
die(mysql_error());
}
?>
<?php
$username=$_POST['menu_name'];
$id=$_POST['id'];
$db_select=mysql_select_db("ocr");
if(!$db_select){
die(mysql_error());
echo" error";
}
$query= "INSERT INTO show (
name , id )
VALUES( '{$username}' ,{$id} ) " ;
if(mysql_query($query)){
header("www.google.com");
exit;
}else{
echo"<p> error </p>";
}
?>
`
and when iam run the site show to my an error what am doing wrong any heleeeep plez ....:))

Here you can find plenty of information about SQLQuering with Joomla!
http://docs.joomla.org/Accessing_the_database_using_JDatabase
You can ask anything you want if you have any trouble...

Related

Not able to insert data using html form and php using wamp [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 1 year ago.
I am trying to insert data into a database from HTML form using php. I made two files html form and other is PHP script. When I click on submit in html form, it shows me the php code. I am using wamp server for database. I put my html files in C:/wamp64/www directory and html files at my local directory. The database table is :
id int(11)
fname varchar(30)
salary int(11) . Id is not auto-incremented and it is a primary key.
Html code:
<html>
<body>
<h2>Employee's Information</h2>
<form action="employee.php" method="POST">
<label for="id">Enter employee id:</label><br>
<input type="text" id="id" name="id" value=""><br>
<label for="fname">Enter First name:</label><br>
<input type="text" id="fname" name="fname" value=""><br><br>
<label for="salary">Enter Employee Salary:</label><br>
<input type="text" id="salary" name="salary" value=""><br><br>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
Php code:
<?php
$mysql_hostname="localhost";
$mysql_username="root";
$mysql_password="";
$mysql_database="employee";
$con=mysql_connect($mysql_hostname,$mysql_username,$mysql_password);
if(!$con){
die('Connection Error: '.mysql_error());
}
mysql_select_db($mysql_database, $con);
if(isset($_POST['submit']))
{
$s_id = $_POST['id'];
$s_name = $_POST['fname'];
$salary = $_POST['salary'];
$employeeinsert = "INSERT INTO employee1
(id, fname, salary)
VALUES('".$s_id."','".$s_name."','".$salary."')";
if(!mysql_query($employeeinsert,$con)) {
echo "Error: " .mysql_error($con);
} else {
echo "1 record added";
}
}
?>
The code is neither giving any error on submitting data nor it is inserting the data into the database.
I am not getting what the error is.
If this is false then the code successfully produces no output:
if(isset($_POST['submit']))
Which is what's happening, since the condition is false. The form has a submit button, but that button has no name attribute to its value isn't sent to the server:
<input type="submit" value="Submit">
Give it a name:
<input type="submit" name="submit" value="Submit">
It's always a good idea to have some kind of indication of any given code branch, even if just logging something somewhere so you can see what's happening. Code will happily produce no output/result if that's what it's instructed to do, but as you've discovered it can leave you with no information about what's happened.
As an aside, and this is important, your code is wide open to SQL injection. You'll want to start addressing that.

Process PHP form on hidden page, but stay on same page. No Ajax, pure php only (project)

I'm new to PHP, and I'm sure this is a common think to do, but 99% of the answers I have found to this involve AJAX, JQuery and/or JavaScript.
I am only allowed to use HTML/CSS and PHP in my project, so I need a working option that does not involve anything else.
I have the following setup:
index.php, this holds my form structure
insert.php, this sanitizes/validates and inserts form data into a database table
Leaving action as insert.php sends me to my insert.php page, which I want to keep private and for developer eyes only...no good.
form action=" " method="post"
// OR
form action="index.php" method="post">
Leaving action blank or as index.php keeps me on the same page, but...
I want to keep my form processing in a separate file (insert.php) and NOT on the same page, if at all possible.
Do I have any options for this that are not excessively complex in pure PHP?
Thanks for any advice.
(PS. If there's any blatant errors or poor form here, I'm all ears to constructive criticism)
Here's a snapshot of my insert.php file if its helpful. I can upload my form as well, but its very basic (just select a course, input first/last name, input student id).
// Clean the data coming from the MySQL tables
$course_clean = htmlentities($_POST['course_id']);
$stu_name_clean = htmlentities($_POST['first_last']);
$stu_id_clean = htmlentities($_POST['stu_id']);
// Escape user input coming from forms
$course = mysqli_real_escape_string($open, $_REQUEST['course_id']);
$stu_name = mysqli_real_escape_string($open, $_REQUEST['first_last']);
$stu_id = mysqli_real_escape_string($open, $_REQUEST['stu_id']);
// INSERT DATA
$insert = "INSERT IGNORE INTO enrolled (course_id, stu_id) VALUES ('$course', '$stu_id')";
if(mysqli_query($open, $insert)){
echo "Records added successfully to ENROLLED.";
} else{
echo "ERROR: Could not add records to ENROLLED. " . mysqli_error($open);
}
Something like this might be what you want:
<?php
if (!empty($_POST)) {
require "insert.php";
}
?>
<html><head></head><body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="text" name="course_id"><br/>
<input type="text" name="first_last"><br/>
<input type="text" name="stu_id"><br/>
<input type="submit">
</form>
</body></html>
It's possible to submit the page to itself and check if the $_POST is empty or not. If it's empty show the form of the page if not insert the data into the database.
<?php if (!empty($_POST)):
// Clean the data coming from the MySQL tables
$course_clean = htmlentities($_POST['course_id']);
$stu_name_clean = htmlentities($_POST['first_last']);
$stu_id_clean = htmlentities($_POST['stu_id']);
// Escape user input coming from forms
$course = mysqli_real_escape_string($open, $_REQUEST['course_id']);
$stu_name = mysqli_real_escape_string($open, $_REQUEST['first_last']);
$stu_id = mysqli_real_escape_string($open, $_REQUEST['stu_id']);
// INSERT DATA
$insert = "INSERT IGNORE INTO enrolled (course_id, stu_id) VALUES ('$course', '$stu_id')";
if(mysqli_query($open, $insert)){
echo "Records added successfully to ENROLLED.";
} else{
echo "ERROR: Could not add records to ENROLLED. " . mysqli_error($open);
}
else: ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="text" name="course_id"><br/>
<input type="text" name="first_last"><br/>
<input type="text" name="stu_id"><br/>
<input type="submit">
</form>
<?php endif; ?>

Database isn't receiving values from form using php

Im trying to send form data to my sql database but the database isn't receiving any of my values. The name of my database is taxibooking and table name is bookings.
I tried separating the php code in another file and using action on form to access the php code. on clicking submit I was redirected to a blank page with my php file name.
<form method="POST" action="">
Name of customer:<input type="text" name="fname"><br><br>
Enter pickup address:<textarea name="padd" rows="5" cols="10"></textarea><br><br>
Enter destination address:<textarea name="dadd" rows="5" cols="10"></textarea><br><br>
Select Taxi type:<select name="taxi"><option value="Viennese Fiaker">The Viennese Fiaker</option><option value="Indian Auto Rickshaw">Indian Auto Rickshaw</option><option value="Little Yellow">Little Yellow</option><option value="Mumbai Taxi Fiat">Mumbai Taxi Fiat</option><option value="Tricycles">Tricycles</option><option value="Water Taxi">Water Taxi</option><option value="Impeccable Taxi">Impeccable Taxi</option><option value="Red Taxi">Red Taxi</option></select><br><br>
<input type="submit" value="submit" name="sub">
</form>
<?php
$con=mysqli_connect("localhost","root","","taxibooking");
if(isset($_POST['sub']))
{
$n=$_POST['fname'];
$p=$_POST['padd'];
$d=$_POST['dadd'];
$type=$_POST['taxi'];
$sql="insert into bookings(name,pickup,destination,type) values ('$n','$p','$d','$type')";
mysqli_query($con,$sql);
}
?>
Try to debug and put query on if condition
if(mysqli_query($con,$sql)){
echo'done';
}else{
echo "error".$sql."</br>" . mysqli_error($con);
}
Here's how to add parameters on your insert script.
$sql="insert into bookings(name,pickup,destination,type) values ('".$n."','".$p."','".$d."','".$type."')";
I made a new database with a different name with the same table name and properties and now it sort of mysteriously works now. No changes done to the code.

Why php code executes 3 times in wordpress

I am very new to wordpress. I am trying to do the following:
a. I have a very simple html and php based website. I have a signup page which accepts user id and password and then on click of submit button it calls a php file which verifies the userid/password and saves it in the database. THIS WORKS PERFECTLY FINE
b. Now, I trying to move those two pages to wordpress platform. I have installed a php plugin also. Now the problem in wordpress platform is whenever I hit the submit button, in the database 3 duplicate records are getting appended. I have been trying a lot but could not fix this
c. I tried different php plugins but does not work
d. After reading few help documents on the web, I have tried creating my own template page, even that does not work
Any help in this regard will be much appreciated.
EDIT
Form:
<form action="http://xxxxxx.xxx/xxxxxx" method="post">
Enter User ID:
<input name="userid" type="text" value="" />Password:
<input name="password" type="text" value="" />
Confirm Password:
<input name="confrimpassword" type="text" value="" />
<input type="submit" value="SIGN UP" />
</form>
Below is the php file:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
//echo "Favorite color is ".$_SESSION["favcolor"].".<br>";
//echo "Favorite animal is ".$_SESSION["favanimal"].".";
?>
<?php
mysql_connect("localhost","***********","**************");
mysql_select_db("******");
$sql1=mysql_query("select * from login_details where customer_id='$_POST[userid]'");
$row=mysql_fetch_assoc($sql1);
if (!$sql1) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
//exit;
}
else {
echo " inside last else";
$sql2=mysql_query("INSERT INTO login_details VALUES ('$_POST[userid]','$_POST[password]')");
echo "Account successfully Created...";
}
mysql_close();
?>
</body>
</HTML>

Data not updating in SQL databse by PHP

Well I am new to coding,What I am trying is to update data in database through Php .I am trying hard to update data but i don't know where is problem coming ,there is no error too.My first file is
"ppp.html"
<html>
<form action="l.php"method="post">
<input type ="text" name ="complaint">
</input>
<input type="submit"></input>
</html>
Now my "L.php "
It also don't show any error .It goes through easily
<?php
$complaint="";
if(
isset($_POST['complaint']))
{$complaint =$_POST['complaint'];}
mysql_connect("localhost","root","") or die ("couldnt attack ");
mysql_select_db("site")or die('i surrender');
$query=("SELECT * FROM site2 where category='$complaint'") or die("couldnt select");
$result=mysql_query($query) or die ('hghyt');
while ($complaint= mysql_fetch_array($result))
{
echo"<td>".'<br>'.$complaint['category']."</tr>";
ECHO"<TR>"."<A HREF='update.php'>"."UPDATE"."</A>";
echo "<br/>";
ECHO"</table>";
}
?>
Sorry for very wrong query and very inappropriate way of coding but I am learning it all by myself through internet
Now my "update.php file"
<html>
<form action="update1.php" method="post">
<input type= "text" name="blue"></input>
<input type= "submit"></input>
</form>
</html>
It also goes of in easy way,and don't show any problem ,now my last file "update1.php"
<?php
$complaint="";
if(isset ($_POST['complaint']))
{$complaint =$_POST['complaint'];}
$blue="";
if(isset ($_POST['blue']))
{$blue =$_POST['blue'];}
mysql_connect("localhost","root","") or die ("couldnyt coibnovdbs");
mysql_select_db("site") or die ("no databse");
$query=("update site2 set category='$blue' where category ='$complaint'") or die ("couldnt attack");
$result=mysql_query($query) or die("kjkk");
?>
Please help me .It is bothering me,I cant find any solution for it.I think problem is in last file only but it is not showing any errors.
Thank you
Considering the flow of your website, update1.php never receives the value in $complaint, hence fails to update. You need to pass the value to it.
For example, the following edits should suffice.
Edit L.php
echo"<td>".'<br>'.$complaint['category']."</tr>";
ECHO"<TR>"."<A HREF='update.php?complaint=".$complaint['category']."'>"."UPDATE"."</A>";
Edit update.php
<html>
<form action="update1.php" method="post">
<input type="hidden" name="complaint" value="<?php echo $_GET['complaint'] ?>"
<input type= "text" name="blue"></input>
<input type= "submit"></input>
</form>
</html>

Categories