Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
<?php
if (isset($_POST['preview'])){
echo $user = $_SESSION['ue'];
echo $title=$_POST['title'];
echo $dis=$_POST['dis'];
echo $a=$_POST['a'];
echo $b=$_POST['b'];
echo $c=$_POST['c'];
echo $d=$_POST['d'];
echo $timespan=$_POST['timespan'];
$sql="INSERT INTO survey (user, title, description, opta, optb,optc,optd) VALUES ('$user','$title', '$dis', '$a' , '$b', '$c', '$d','timespan')";
if (mysqli_query($con,$sql))
{
echo "Success";
}
else
{
echo "Error: " . mysql_error();
}
mysqli_close($con);
}
?>
Here is my code, all post variables are showed on web page, while data is not inserted in database table. also it does not show any error or exception.
INSERT INTO survey (...) - there is 7 columns
VALUES (...) - and you are sending 8 variables
You didn't escape your $_POST variables
Your connection to database is missing
mysql_error() will not show the errors thrown by the MySQLi functions
There are too many values added: 8 instead of 7
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'd like to have a function that creates database-tables:
input:
$tableName
$tableFields (parameters array)
output: new DB TABLE
The two dimension array contains the fields detentions:
a[0][0] = firstName
a[0][1] = varchar[25]
a[1][0] = lastName
a[1][1] = varchar[30]
The output is a table named $tableName with two fields firstName and lastName.
I think that I can build a create query by looping the array, yet I don't know how to tell php to commit this command and actually create the DB table.
function createTable( $tableName,$a) {
global $con; //database connection
$query = "CREATE TABLE $tableName ";
$temp = "";
for($i=0;$i<count($a);$i++) {
$temp .= $a[$i][0] . " " . $a[$i][1] . ",";
}
if($temp!='') {
$query .= substr($temp,0,-1);
mysqli_query($con,$query);
}
else {
print "No column names provided";
}
}
just have a look into the mysqli module of PHP: http://www.php.net/manual/de/book.mysqli.php
There you can create a connection to a database, perform queries etc.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I made a new table , everything worked.
CREATE TABLE IF NOT EXISTS logdata (
email varchar(30),
password varchar(20),
username varchar(15),)
Inserted the id auto increment code
,and some data :
INSERT INTO logdata(email,password,username,id) VALUES('test#test.org','testtest1','test',' ')
Everything worked here. When I try to output the data i dont get any results (except "ERROR"). I have no idea why.
<?php
error_reporting(E_ALL);
// here is where I set the connection , everything is working here
if(mysqli_connect_errno()){
echo "Could not connect to the database <br /><br />";
echo mysqli_connect_error();
exit();
}
$dostuff="SELECT * FROM logdata";
$query = mysqli_query($db_conn, $dostuff);
if($query == TRUE) {
echo "Succes!";
}
else{
echo "ERROR ";
echo mysqli_error($db_conn);
}
?>
In order to query something in your database, you have to provide a query to it. Your query variable is an empty string!!
$dostuff="";
It should have some SQL statements, like e.g:
$dostuff="SELECT * FROM logdata";
Or whatever.
UPDATE
I believe that using === to test the result will fail because the mysqli_query returns a mysql_result object, according to the docs:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
So if its succeful it won't be === TURE for your SELECT statement and it will have no error. Your query is fine, just try this:
if ($query = mysqli_query($db_conn, $dostuff)) {
echo "Success!";
}
else {
echo "ERROR ";
echo mysqli_error($db_conn);
}
It should works.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to select a table and I don't know what I am doing wrong:
$result = mysql_query('SELECT * FROM sfat WHERE done="0" LIMIT 0,10');
$row = mysql_fetch_array($result)
$url = $row["web"];
use
while ($row = mysql_fetch_array($result) ) {
$url = $row["web"];
echo $url;
}
in stead of
$row = mysql_fetch_array($result)
$url = $row["web"];
Because your query indicates you are expecting up to 10 rows. But your code will only show the first one.
The following example will be used to create database
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
{
echo "Database my_db created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
if (mysqli_connect_errno()==0)
{ // if successfully connected
$sent_dt=$_POST['scts'];
// important: escape string values
$txt=mysqli_real_escape_string($con, $_POST['text']);
$snd=mysqli_real_escape_string($con, $_POST['sender']);
// creating an sql statement to insert the message into the SMS_IN table
$sql="INSERT INTO SMS_IN(studentID,lastname,firstname,class) VALUES ('34','Lekan','Balogun','Grade8')";
// executing the sql statement
$insert_sms_success = mysqli_query($con,$sql);
// closing the connection
mysqli_close($con);
}
Please, I'm new to php and I got this code from a friend for my project. I want to split the text message using spaces (e.g 34 Lekan Balogun Grade8) before its inserted into my database. My table fields are: StudentID, LastName, FirstName, Class and so I will want it posted as 34 for ID, Lekan for Lastname, Balogun for Firstname and Grade8 entered into class. Thanks in anticipation.
You can use explode() funnction in php for this
$str = '34 Lekan Balogun Grade8';
$arr = explode(' ',$str);
echo '<pre>';
print_r($arr);
You can split the string values using "explode" function,
$textArray = #explode(" ",$txt);
echo "<pre>";
print_r($textArray);
echo "</pre>";
This is you complete code
if (mysqli_connect_errno()==0)
{ // if successfully connected
$sent_dt=$_POST['scts'];
// important: escape string values
$txt=mysqli_real_escape_string($con, $_POST['text']);
$snd=mysqli_real_escape_string($con, $_POST['sender']);
$arr = explode(' ',$txt);
echo '<pre>';
print_r($arr);
$StudentID = $arr['0'];
$LastName = $arr['1'];
$FirstName = $arr['2'];
$Class = $arr['3']
// creating an sql statement to insert the message into the SMS_IN table
$sql="INSERT INTO SMS_IN(sms_text,sender_number,sent_dt) VALUES ('$txt','$snd','$sent_dt')";
// executing the sql statement
$insert_sms_success = mysqli_query($con,$sql);
// closing the connection
mysqli_close($con);
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database table, I have a lot of questions ,each having a category . Say, there are 100 questions and some number of categories which I don't know . I want to know the number and the names of the categories.
Please tell the way to do it in php.
You can try something like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT category_name FROM your_table GROUP BY category_name");
$count = mysqli_num_rows($result); // no of categories
while ($row=mysqli_fetch_row($result)) {
echo $row[0]; // printing category name
}
How about...
<?php
$mysqli = mysqli_connect( /* info */) or die ("MySQL error");
$result = mysqli_query("SELECT COUNT(DISTINCT `category`)");
echo $result;
?>
That is assuming categories are all in a single column.