Unable to connect Database to HTML form - php

Well I build a form in html but I am unable to connect and collect data from the form to a database. Here is the code I used to connect and collect data from the form.
The HTML form -> https://drive.google.com/file/d/0B4eL9qVqrm2OOWpMZmV1dUlNbTg/view?usp=sharing
The PHP connect file ->
https://drive.google.com/file/d/0B4eL9qVqrm2OY2pDR09nb0w5bE0/view
The PHP database file ->
https://drive.google.com/file/d/0B4eL9qVqrm2OeU5IdVMxVHBkaVk/view
It returns the code back on clicking submit.
<?php include 'NENDatabase.php';?>
<?php
$Your_name=$_POST['Your_Name'];
$email=$_POST['Your_Email_ID'];
$Message=$_POST['Type_your_Message'];
$Gender=$_POST['sex'];
mysqli_query($connect, "INSERT INTO Visitors(Your_Name,Your_Email_ID,Message,sex)
VALUES('$Your_name','$email','$Message','$Gender')");
if(mysqli_affected_rows($connect) > 0){
echo "<p>People visited</p>";
echo "Go Back";
} else {
echo "People Added<br />";
echo mysqli_error ($connect);
}
?>
I have no idea about collecting data into databases and used the php codes from tutorials online. Still the code doesn't run. Any help or push in the right direction will be very helpful.

Found two errors
Error 1 - change form method to post, because php code uses $_POST
Error 2
mysqli_query($connect"INSERT INTO Visitors(Your_Name,Your_Email_ID,Message,sex)
VALUES('$Your_name','$email','$Message','$Gender')");
function parameters not seperated by comma, so change it to
mysqli_query($connect,"INSERT INTO Visitors(Your_Name,Your_Email_ID,Message,sex)
VALUES('$Your_name','$email','$Message','$Gender')");

Hmm the problem was not with the code but with the url path that I was using. I just had to change the paths to localhost/filename.php or localhost/site.html so that it is actually hosted and run on the localhost server. You can also use absolute servers instead of localhost. (And make sure all your files are in HTACCESS folder under xampp directory of mysql)

Related

Can I write all my SQL queries to phpMyAdmin / HTML-file or do I need to use other programmes?

I have a database in phpMyAdmin that I have set up with XAMPP. I am working on a website that shows statistics from the user inputted scores in the database. Say that I would like to show the score percentile to the user after they submit their score: where do I write the query for that? In the HTML/PHP code? In phpMyAdmin? Somewhere else like a workbench or PopSQL?
I have successfully gotten the website to display the average score in any given table by writing this code to the HTML file:
<?php
$sql = "SELECT AVG(score) AS score FROM $input_subject";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($result) ;
echo nl2br("Average score for $input_subject: \n \n"
. round($row->score));
?>
It works, but when I search for tutorials for example for the percentile query or something like the CHECK function (to not accept anything less than 0 or more than 120) it seems that the queries are always written somewhere else than the HTML file.
Also, when I try to write the SQL code in phpMyAdmin, it always shows a bunch of error messages, even though I copy/pasted it in (changing the table names etc., of course).
So, do I need to look into some other programmes were to write the queries in or can I just write them into the HTML-file or in the phpMyAdmin? I'm a total newbie with this so anything helps!
Yes, you can write SQl queries(PHP) before your html code and also in between your html code, but make sure to change file extension to .php from .html otherwise PHP code will be printed on browser as it is.
"it seems that the queries are always written somewhere else than the HTML file."
we do this while using AJAX. we send data to server (a PHP file where we process data received) and server will give response. All this happens behind the scenes without page reloading.
in your case you can create a new PHP file lets say process.php, add your PHP code into it.
process.php
<?php
$sql = "SELECT AVG(score) AS score FROM $input_subject";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($result) ;
echo nl2br("Average score for $input_subject: \n \n"
. round($row->score));
?>
send user inputted scores from HTML file through ajax to PHP file (process.php) and response received from that file can be displayed in html file without page reloading.
you can go through the ajax api by following link jQuery-AJAX

Mysterious issue in my php script

I've been developing a news letter system locally and everything works fine . The mystery begun when I uploaded the whole content to my server , I could see the index page but when I enter the username / Password to log in (in the index page), I get redirected to "authenticate.php " where the Post data are processed this way :
<?php include("includes/initialize.php");
if(!isset($_POST['username']) OR !isset($_POST['password']))
{
// no data were given
redirect_to("error.php");
}
elseif(!$user = User::authenticate($_POST['username'],$_POST['password']))
{
// user not found in the database
redirect_to("error.php");
}
else {
$session->login($user);
redirect_to("enewsletter.php");
}
?>
On this page I get stuck (not on localhost ) , the server is rendering a blank page . after some tests , I tell you what happens : If I fall in the if situation i get redirected to error.php , but when I enter some credentials , It's doing nothing :/
So I added an echo statement to keep trace of the code :
and I had this remark, every time the script meets to a line containing a method that looks in the database (OOP approach) the server is always rendering blank pages , I tried it on many pages and it's always the same . I changed the database connection type from PDO to mysqli (because I thought the server is not supporting PDO) but no change !!
Does any one has a clue , what could be the reason?
well ,
it was just a " Self instead of self " problem ...
Thank you all for your help ;)

mysql data not being updated

Whilst moving from one host to another (to hostgator) we've received many errors. We did have a news editor edit code error once we moved, but we've fixed that. Now, we have an error updating data in page editor.
It can pull/select data and says saved, but it doesn't update the data in mysql.
Hosting error? was working before.
»Events Page
»Jobs Page
»Maintenance Page
»Staff Page
Thank you,
Maybe your previous host had autocommit=on, meaning you do not have to COMMIT.
Try a COMMIT statement at the end of each data update to actually save the data
echo("The CMS for the credits page has been updated!");
is just the php saying what you want it to say, it did not actually go into the database and check...
use
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
echo("maintenance page updated");
}
to identify that your query is working fine or not

php mysql query in function not working

I have a mysql query that returns sum of values in multiple columns. The query is right and everything is working normally when i include it in the main page. But the problem starts when i make a function in another
page include the query over there and return the sum and print it in main page its not working .
Below is the main page call :
require('totals.php');
$res_easyhint_total=easyhint_totals($currentpid);
print $res_easyhint_total;
//The above is contained in a while loop and current pid gets updated each time.
Function page:
function easyhint_totals($currentpid){
require('connect.php');
$sql_easyhint_total = "SELECT sum(Coffee+Gift+Cools+Affection+Patience+Anger+EHignore) from whyangry.posts where Pid=$currentpid";
$res_easyhint_total=mysql_query($sql_easyhint_total,$con);
$res_easyhint_total=mysql_fetch_array($res_easyhint_total);
$res_easyhint_total=$res_easyhint_total[0];
return $res_easyhint_total;
}
I dont get what the error is please help.
Do you define any functions in connect.php? If not try adding this:
$res_easyhint_total=mysql_query($sql_easyhint_total,$con);
if (mysql_errno() != 0) {
echo mysql_error();
}
$res_easyhint_total=mysql_fetch_array($res_easyhint_total);
Have you checked the result from the new page there itself? I mean to say if you tried to print the result from the new page itself as like you tried from main page. Then one more thing need to concetrate that, the path of both file while including. Try to pass any other variable from the new page to main page and check if the new file included properly.
If you are able to access other variable from new page on main page and its just not returning the result from the function. Try to include connect.php on main page also and check it.
Check your connection string is returning the proper linked identifier and also check the logs if there is any error or warning from mysql like Warning: mysql_query(): [2002] No such file or directory (trying to connect via unix:///var/run/mysql/mysql.sock). In that case try to set the proper socket file location.
Have you included the file "connect.php" before?
if(!#include_once('connect.php')) {
// include connect.php
}

Echo SQL query on new page with PHP

I know this is probably a really amateur question, but I can't figure this out and I don't know much about PHP or MySQL.
So I have a really simple script that basically allows a user to submit a couple lines of text and their zipcode, then it write it to a database and returns the results on the site. It's a shoutbox essentially.
My client wants the users to be able to filter the results by zipcode. So I have it all setup, and I have a search input where people type in their zipcode, search, and then the PHP returns the submissions from that zipcode.
While I can get the results to show by themselves echoed from the PHP script, how do I get them to display within a specified div within the site? I want it essentially to store a variable, the zipcode, that a user search by, and then use that once the page refreshes to display an updated list that filters out the results that aren't from that zipcode.
Thanks so much for the help.
Chris
Without getting into rewriting etc.:
When you redirect your page, add a query at the end of it that is the zipcode making sure that you don't send any whitespace and that you have an input in a given format:
search.php?zipcode=90210
Then on your search results page, fetch the variable passed and work as usual:
$zipcode = $_GET['zipcode'];
make sure that the zipcode is properly escaped and filtered for nasties.
Make the HTML page a PHP page (usually by making the extension .php). Replace the target div's content with <?php stuff to get and print results ?>.
If you have PHP installed and configured on the web server, it will take certain files (usually those ending in .php) and run them through PHP's interpreter. The PHP interpreter is invoked anytime there is a <?php in the document and it stops parsing content anytime it meets a ?> within the <?php block.
For instance, to get a web page to print "Hello World!" into a div through PHP, I would do:
<div><?php
print 'Hello World!';
?></div>
OK, I figured it out. I just stored my answer in a SESSION variable so that I could call it in my other PHP files.
Now I have one file processing the request, creating the session and the variable, and then redirecting the page. Then I have a PHP included in the page that grabs the SESSION variable and plugs it into my mysql_query to return the proper result!
Not sure if this is the best way of doing it, but it's working.... If anyone knows of a more elegant solution I would love to know it.
Thanks,
Chris
I suggest you do some tutorials on using PHP and database retrieval with mysqli.
<?php
/* Connect to a MySQL server */
$link = mysqli_connect(
'localhost', /* The host to connect to */
'user', /* The user to connect as */
'password', /* The password to use */
'world'); /* The default database to query */
if (!$link) {
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
/* Send a query to the server */
if ($result = mysqli_query($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) {
print("Very large cities are:\n");
/* Fetch the results of the query */
while( $row = mysqli_fetch_assoc($result) ){
printf("%s (%s)\n", $row['Name'], $row['Population']);
}
/* Destroy the result set and free the memory used for it */
mysqli_free_result($result);
}
/* Close the connection */
mysqli_close($link);
?>

Categories