How to split strings using space before posting into mysql [closed] - php

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);
}
?>

Related

Data is not inserted in mysql database [closed]

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

Create PHP table from array [closed]

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.

i have an error while inserting the values what i did [closed]

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
help me with this code i am new to php
<?php
$conn=mysql_connect("localhost","root","","test");
if(isset($_POST['submit']))
{
$sql="INSERT INTO registration(fname,designation,emailid,
address,phonenumber)VALUES('".$_POST['fname']."','".$_POST['designation']."','".$_POST['ema
lid']."', '".$_POST['address']."','".$_POST['phonenumber']."')";
echo $sql;
$result=mysql_query($conn,$sql);
echo $result;
}
else{
echo "Error";
}
?>
its a registration page getting values and inserting it in the table...
You have the parameters around the wrong way here:
$result=mysql_query($conn,$sql);
Try
$result=mysql_query($sql, $conn) or die(mysql_error($conn));
Side notes:
Don't use mysql_*() functions: they're deprecated. Use mysqli_*() versions instead.
You should escape your user inputs with mysql_real_escape_string() to protect against SQL Injection attacks. Consider using prepared statements with mysqli_() instead.
Take a look at this link which is a good tutorial for inserting data (from a form etc.) to a mysql database.
Also: be aware of sql-injection and prevent it. here is a tutorial on how to do this: link
If you want to have readable code, set the $_POST[] values to a variable, and then pass them to the query, it's not different in fact but this is more easy and clean.:
<?php
$conn=mysql_connect("localhost","root","","test");
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$designation = $_POST['designation'];
$emailid = $_POST['emailid'];
$address = $_POST['address'];
$phonenumber = $_POST['phonenumber'];
$sql="INSERT INTO registration(fname,designation,emailid,address,phonenumber)";
$sql .="VALUES('$fname', '$designation', '$emailid', '$address', '$phonenumber')";
echo $sql;
$result=mysql_query($conn,$sql);
echo $result;
}
else{
echo "Error";
}
?>
you hade a typing mistake in $_POST['emailid']...
and you can select your database with this:
mysql_select_db('your db name');
put this line after your connection variable means $conn
and this is wrong:
$result = mysql_query ($conn, $sql)
you have to set the query first:
$result = mysql_query($sql, $conn)

PHP: How to check if no rows are returned [closed]

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
With the following PHP code, how do i check if there's no row retuned so i can echo some message?
PHP:
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
while($row = mysql_fetch_array($Result)){
echo '<span class="classPid" style="display:none">'.$row['pid'].'</span>';
}
?>
Thanks
There are a number of ways to do this. There is a specific function mysql_num_rows() which will return the number of rows returned.
mysql_num_rows($Result);
This will return 0 if there are no rows affected or returned.
You could also create some conditions using mysql_fetch_array. mysql_fetch_array will return FALSE if there are no rows.
On a separate note it would be a good idea to update your connection
and functions to mysqli as mysql is no depreciated.
See docs on mysql_num_rows().
http://www.php.net/mysql_num_rows
Something like this
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
$i=0;
while($row = mysql_fetch_array($Result)){
echo "<span class='classPid' style='display:none'>".$row['pid']."</span>";
$i++;
}
if($i==0){
echo "No rows found";
}
?>

Reading data from mysql table [closed]

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.

Categories