I can't add strings to the data base, I don't know why, but only empty strings are added.
I think that the main problem is in this part of code (PHP):
$word=$_POST[".$i."];
mysql_query("INSERT INTO words(word) VALUES('$word')",$connection);
I need to add several strings, that I get from (text input)s to the data base
the whole code is here:
<html>
<head>
<title>Admin Panel (Second Page)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
error_reporting(0);
$user="root";
$pass="";
$connection = mysql_connect("localhost",$user,$pass);
mysql_query("CREATE DATABASE IF NOT EXISTS quiz",$connection);
mysql_select_db("quiz",$connection);
mysql_query("CREATE TABLE IF NOT EXISTS words(id MEDIUMINT NOT NULL AUTO_INCREMENT,word TEXT,PRIMARY KEY(id))",$connection);
$number = $_POST['numberOfWords'];
echo "<form action=SecondAdminPage.php method=POST ENCTYPE='multipart/form-data'>";
for($i=1; $i<=$number; $i++)
{
echo "<input type='text' name='$i'>";
$word=$_POST[$i];
mysql_query("INSERT INTO words(word) VALUES('$word')",$connection);
}
echo "<input type='submit' value='Save'>";
?>
</form>
</body>
</html>
Thank you
Remove the error_reporting(0) line to see any PHP errors in your page.
The connection might not be established properly.
Also, the mysql extension is deprecated. Use mysqli instead.
Related
I am building an html form that will need to dynamically fill in the first input field with the "facility" column values in my "doctors" table. The facility column contains the names of our 7 offices. However, when I run the code below, my input field is blank and I have verified there is data in my "doctors" table. After this is working, I need to be able to dynamically fill in the second input field (which I haven't coded for in the code below because I'm stuck with the issue of first input) with the "provider" column values, also from my "doctors" table. The "provider" column contains all the provider names in our practice. However, the providers should be filtered, so that only the providers at the facility from the first input field is showing.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
$link = mysqli_connect("localhost","USERNAME","PASSWORD");
mysqli_select_db($link,"DB");
?>
<head>
<title> Untitled Doc</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name "form1" action="" method="post">
<select>
<?php
$res=mysqli_query($link,"select facility from doctors");
while($row=mysqli_fetch_array($res)){?>
<option> <?php echo $row ["facility"]; ?></option>
<?php }?>
</select>
</form>
</body>
</html>
It is important that you check if the connection have been established, as I have copied your code as it is and use it on my side and was working fine, therefore made me suspect that the problem might be connection related. also check how sensitive your server is maybe your server see this as an error : $row ["facility"] that space might be the problem as well, but it didn't on my side.
Check your server error log and also enable error reporting at the top of your page add
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);?>
That will enable error reporting, but use that on local server only
Then on live site send them to error log
error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);
also to get the mysqli errors, before your connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Important to check if your query does indeed return results before trying to display them.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
$link = mysqli_connect("localhost", "root", "", "DB");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
<head>
<title> Untitled Doc</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name ="form1" action="" method="post">
<?php
$query = "SELECT facility FROM doctors";
if ($res = mysqli_query($link, $query)) {
echo "<select name=\"myselect\">";
while ($row = mysqli_fetch_assoc($res)) {
?>
<option value="<?php echo $row['facility'];?>"><?php echo $row['facility'];?></option>
<?php
}
echo "</select>";
mysqli_free_result($res);
} else {
printf("Error : %s\n", mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>
</form>
</body>
</html>
NB: For your own benefit, if you haven't used prepared statements, would suggest that you learn them as well, though they are not needed
in this case
When doing this kinda of query its important to catch errors so that you can debug easier. I have added a different way of connecting using mysqli object. Try this, this should determine if you have a connection error or if your query is not returning any results
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
$link = new mysqli("localhost","USERNAME","PASSWORD", "DBNAME");
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<head>
<title> Untitled Doc</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name "form1" action="" method="post">
<select>
<?php
$res = $link->query("select facility from doctors");
// Check to see if query returned results
if($res->num_rows > 0 {
while($row = $res->fetch_assoc())
{
echo '<option>' . $row["facility"] . '</option>';
}
} else {
echo 'No results';
}
?>
</select>
</form>
</body>
</html>
Based on your code, when you want to access the data in a array you should put no space between the variable and the square brackets [] or your application will not show anything. In your code when you want to echo use
<option><?php echo $row["facility"]; ?></option>
instead of
<option><?php echo $row ["facility"]; ?></option>
For another case of the second field depends on the first field, i suggest to use JavaScript to get the data by sending the first data because it will reduce the processed of getting the data.
UPDATED:
To access your data, please add another parameter in your mysqli_fetch_array to be like this
while($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
this will make your data can be access using name on your table
<select>
<?php
$res=mysqli_query($link,"select facility from doctors");
while($row = $result->fetch_assoc($res))
{
?>
<option> <?php echo $row ["facility"]; ?></option>
<?php
}
?>
</select>
I'm trying to write to a database using CKEditor.. when I press submit it dies and says localhost is currently unable to handle this request.
HTTP ERROR 500
I only want to save the textarea into a row in database so I can then read the row on to another page.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<title>Classic editor replacing a textarea</title>
<script src="http://cdn.ckeditor.com/4.6.0/standard-all/ckeditor.js"></script>
</head>
<body>
<form id="editor1" action="save.php" method="post" >
<textarea cols="80" id="editor1" name="editor1" rows="10">
</textarea>
<p>
<input type="submit" value="Submit">
</p>
</form>
<script>
CKEDITOR.replace( 'editor1' );
</script>
</body>
</html>
PHP script
<?php
if(isset($_POST['submit']))
{
// Putting data from form into variables to be manipulated
$text = $_POST['editor1'];
$conn = mysql_connect("localhost","root","root") or die ("Can't connect");
mysql_select_db("managerMessage",$conn);
// Getting the form variables and then placing their values into the MySQL table
mysql_query("INSERT INTO text (textarea) VALUES ("mysql_real_escape_string($text)");
}
?>
You are not concatenating the value correctly in this statement and also text data in a query like this should be wrapped in quotes
mysql_query("INSERT INTO text (textarea) VALUES ("mysql_real_escape_string($text)");
This is a corrected verion of your code
mysql_query("INSERT INTO text
(textarea)
VALUES ('" . mysql_real_escape_string($text) . "')");
A simpler piece of code would be to read and probably maintain would be
$t = mysql_real_escape_string($text);
mysql_query("INSERT INTO text (textarea) VALUES ('$t')");
I would be remiss if I did not remind you that
Every time you use the mysql_
database extension in new code
a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7.
If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions.
Start here
EDIT RE: not saving the data to the database
Add some error checking to your code like so:
$t = mysql_real_escape_string($text);
$result = mysql_query("INSERT INTO text (textarea) VALUES ('$t')");
if ( ! $result ) {
echo mysql_error();
exit;
}
Once you know the error, if one exists, you can start work on fixing it.
I'm very new to PHP- and am trying to teach myself a bit of web programming with hands-on learning. I've stumbled upon this: https://www.youtube.com/watch?v=CS45YAqCgX8 video series on how to build a comment section, but am stuck.
My First PHP file looks as such:
<?php
require('connect.php');
If(isset($_POST['submit']))
{
$name=$_POST['name'];
$comment=$_POST['comment'];
If(name&&comment)
{
$insert=mysqli_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')");
}
}
/*else
echo "Please fill out your name and leave a comment.*/
?>
<html>
<br/>
<br/>
<body>
<form action = "CommentForm.php" method = "post">
<table>
<br/>
<tr><td>Name: </td><td><input type = "text" name = "name" size = "30"/></td></tr>
<tr><td colspan="2">Comment:</td></tr>
<tr><td colspan="2"><textarea name = "comment"></textarea></td></tr>
<tr><td colspan="2"><input type = "submit" name = "submit" value = "Comment"/></td></tr>
</table>
</form>
</body>
</html>
The connection file is:
<?php
mysqli_connect("My Host", "My Username","My Password");
mysqli_select_db("My Database");
?>
-and the HTML I want to attach it to is as such
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Pokemon</title>
<link rel = "stylesheet" type = "text/css"
href = "stylishandviewtiful.css">
</head>
<body>
<br/>
<br/>
<br/>
<br/>
<h3>Competitive Meta and Community</h3>
<h3>In-Game Story Ideas and Opinions</h3>
</body>
<?php include 'CommentForm.php'; ?>
</html>
I'll suppose I can't just use an include function? Would I see a better return to separate the HTML in the first PHP application into its own file?
I may generally need a tutorial for this specific instance that starts slow enough for any layman to follow.
You forgot to put the connection inside the mysqli_query($con,"insert ...")
otherwise your insert will be linked to $link. second try making connection like this $con=mysqli_connect("localhost","my_user","my_password","my_db"); pass your $con to your mysqli_query
see http://www.w3schools.com/php/func_mysqli_query.asp
and if your form still does not appear you need to look at your include file. If there is something wrong it will abort
I am trying to build a simple search script in my own customer portal, I am trying to build it to search by their phone ESN. I am not very experienced with php, so I am probably missing something small, or making a small mistake.
anyway, when you type in any esn of a phone and hit search, it shows no results at all. here is the simple script I am working with
<?php
$dbserv='********';
$dbuser='********';
$dbpass='********';
$dbdata='********';
$link = mysql_connect($dbserv, $dbuser, $dbpass);
if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db($dbdata);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="esn" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if (!empty($_REQUEST['esn'])) {
$esn = mysql_real_escape_string($_REQUEST['esn']);
$sql = "SELECT * FROM phones where phoneserial LIKE '%".$esn."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'ESN: ' .$row['phoneserial'].'<br>';
}
}
?>
</body>
</html>
I noticed I made an error earlier in this line
$esn = mysql_real_escape_string($_REQUEST['esn']);
I had it like this instead
$term = mysql_real_escape_string($_REQUEST['esn']);
and then when you typed a 14 digit esn starting with A10000, it would actually show every esn in our database instead of the one that was searched for and i don't know why.
Forgive me if this is lame, still trying to learn
If $esn was undefined, you'd be searching for a pure wildcard (%%), which would match every possible record. Not that surprising when you look at this code:
$sql = "SELECT * FROM phones where phoneserial LIKE '%".$esn."%'";
Just remove the $esn since you didn't define $esn and instead used $term.
You really shouldn't use mysql_ functions anymore. Learn how to use mysqli or PDO and prepared statements rather than escape string functions.
if u want to find only ONE, strict match, you should replace your query with this
$sql = "SELECT * FROM phones where phoneserial = '{$esn}'";
Don't use mysql, use Mysqli
MySQL extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
Final well form code is
<?php
$dbserv='********';
$dbuser='********';
$dbpass='********';
$dbdata='********';
$link = mysqli_connect($dbserv, $dbuser, $dbpass,$dbdata);
if (!empty($_POST['esn'])) {
$esn = mysqli_real_escape_string($link$_POST['esn']);
$sql = "SELECT * FROM phones where phoneserial LIKE '%".$esn."%'";;
$r_query = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($r_query))
{
echo 'ESN: ' .$row['phoneserial'].'<br>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="esn" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
I'm new to PHP, HTML and MySQL, and have encountered the following problem:
I have a PHP document which outputs the results of a MySQL query. Because the MySQL database and thus the output results have some non-standard characters (such as æ or á), my PHP document and the MySQL database/tables are encoded as utf-8.
For instance, here is an example of the a database entry and correct output:
goahteæjgáda
When the PHP document does not have anything in the HTML <head/> node (not even comments), then the search is successful and the output is displayed correctly (but then I can't apply my external css or include the shortcut icon, etc.).
However, if there is anything at all in the HTML <head/> node, such as standard metadata concerning content type, links to css and icon files, keywords, or even just <!-- comments -->, then either:
the search does not work if the string being searched for contains a non-standard character
OR
any non-standard characters in the resulting output are displayed as � -- for instance, the example above shows up like this after searching for "goahte":
goahte�jg�da
Any help would be appreciated!
Here is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href='style_mavsulasj.css' rel='stylesheet' type='text/css'/>
<link rel='shortcut icon' href='farben4.gif'/>
<title>search</title>
</head>
<body>
<div style="width:230px;padding:0px;margin:0px;float:left;">
<table border="0">
<tr><td>Search here:</td><td>
<?php if (strlen($_GET['smj'])==0) echo ""; else echo "current search"; ?>
</td></tr>
<form action="" method="GET">
<tr><td colspan="2">Entry:</td></tr>
<tr><td><input type='text' name='smj' value=''></input></td><td align='center'><?php echo "<span class='searchCrits' > ".$_GET['smj']."</span>"; ?></td></tr>
<tr><td colspan="2"><input type="submit" value="submit query"/></td></tr>
</form>
</table>
</div>
<div style="width:960px;padding:30px;margin-left:210px;">
<?php
if($_GET){
$smj = $_GET['smj'];
$connect = mysql_connect("localhost","root","root");
if($connect){
$toDB = mysql_select_db("bigG_reimport_test",$connect);
if($toDB){
$query = "SELECT * FROM reimport_Sheet1 WHERE smj LIKE '" . $smj . "%'";
$results = mysql_query($query);
echo "<span class='header4'>results:<br/>";
while($row = mysql_fetch_array($results)){
echo "-> " . $row['smj'] . "<br/>" ;
}
echo "</span><br/>";
}
else {die("Failed to connect to database!<br/>" . mysql_error());}}
else {die("Failed to connect to mysql!<br/>" . mysql_error());}}
?>
</div>
</body>
</html>
Make sure:
Your PHP source file is encoded as UTF-8 (yes, this really matters, sadly)
You've set the charset in your MySQL session to UTF-8. See mysql_set_charset
You set the encoding to UTF-8 in your HTTP headers.