how to insert html table data per tr into sql table - php

here is my following code, I want to insert html table data per row into sql, but using this code, only store last trow to database table, I think I need to use loop to read the data per trow, not only last trow, any ideas?
<?php
//connect
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","Matt","password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("propereva",$con);
INSERT INTO properevatables (Number, Name,BasicSalary,ServiceMonths,SalaryPercentWithoutCEO,SalaryPercentWithCeo,PercentEvaShareByCeoDirectReports,PercentEvaShareByCeoAndDirectReports,PercentKpi,EvaBonusInMonths,percentwithbank,TotalEvaBonus,totalwithbank) VALUES ('$_POST[number]','$_POST[membername]','$_POST[salary]','$_POST[service]','$_POST[percentwithoutCEO]','$_POST[percentwithCEO]','$_POST[ceoReport]','$_POST[directresult]','$_POST[kpi]','$_POST[resultpercentageEVA]','$_POST[percentwithbank]','$_POST[yearonly]','$_POST[totalwithbank]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>

What you are trying to do is send the data of an entire table to the server for storage.
You can do this using JavaScript and collate all the data into a JSON Object to send to the server using an AJAX request.
You can learn about AJAX in JavaScript from
here https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started
or you can chose to use a JavaScript library such as JQuery to help you with your AJAX needs.
http://api.jquery.com/jquery.ajax/
You can also use HTML inputs within the table to submit all the values however this is not recommended.

<?php
include 'connect.php';
if(isset($_POST['submit']))
{
$number=$_POST['Number'];
$name=$_POST['Name'];
$salary=$_POST['BasicSalary'];
$service=$_POST['ServiceMonths'];
$insert=mysql_query("insert into `properevatables ` (Number,Name,BasicSalary,ServiceMonths) values('$number','$name','$salary','$service')");
if($insert)
{
$msg="Inserted Successfully";
echo "<script type='text/javascript'>alert('$msg');</script>";
}
else
{
$msg=" Insert Not Successfully";
echo "<script type='text/javascript'>alert('$msg');</script>";
}
}
?>
By this you can add more fields.I added only three as of now.Here the connect.php is the connection file to the database.You can either use separate file else include he connection code in the same file.

Related

How to give validation with statement of value on database table

i've problem while i make an insert function using php. i dont know how to give a validation while inserting data to database. my insert function using of the other data table from the database (copying data from other table to another).
somebody know how to give a validation with that data. not from a textbox value or input value from the form. but validation from the data in the database.
please help me out my trouble. im very appreciated when you give me an example.
this is my code for insert data (copying data from the other table to another).
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "silo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql="Insert into termocouple
Select * from temp1";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>

mysql php displays results but not inserting into database

I stripped down query for only one insert
<?php
session_start();
include 'cstring.php';
$title="";
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
$title=$_POST['title'];
$query=mysqli_query($con,"insert into blogpages(blogpagetitle) values('".$title."')");
if($query){
$bloga="sucessfully added a new blog";
echo $bloga;
}
else {
echo mysqli_error($con); // if using mysqli do not use mysql in between
}
}
mysqli_close($con);
?>
is there something wong in this code that it doesnt insert into mysql
table structure
1.bpid int(50)--------------null-no default-none autoincrement
2.blogpagetitle------------varchar(255) utf16_general_ci
3.datemade-------------timestamp current time stamp
4.blogpagedescription---------text utf16_general_ci
5.blogbody----------------longtext utf16_general_ci
6.blogpageextended------------ text utf16_general_ci
TIP
Sanitize variables, Use mysqli_real_escape_string()
When you are not able to debug your code, echo every possible stuff and die the rest code.
For example here, echo if there is error in DB connection, echo the query to see if it is correct, echo the result of query execution, echo if there is some error!
You should be using echo mysqli_error($con) to get the error message rather than mysql_error().

php script isnt opening the welcome page nor writing to the database with wamp

Am not being able to get any information into my wamp server database nor will it display the welcome page below is my code can someone please tell me what am doing wrong or if my wamp maybe configured wrong?
php code below
<?
// Test if submit button was pressed
if (isset($_POST['sub_button'])) {
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$sex=$_POST['sex'];
$address=$_POST['add'];
$email=$_POST['e_address'];
$years=$_POST['n_years'];
$favSong=$_POST['f_song'];
// Create the connection to the server
$db_host="localhost";
$db_username="root";
$db_password="";
$con=mysqli_connect($ db_host, $db_username, $db_passwod); // to connect to the database server
// to check the connection to the server
if(mysqli_connect_errno($con)) {
echo"Failed to connect to my SQL:" .mysqli_connect_errno();// if there is an error this shows up.
}
// this selects the database
mysqli.select_db($con, 'fan'); // ensure that the database has the same name as your created database.
//Insert Statement
//Values of fname, lname etc is the exact name spelling from the html file.
//Values of $title $fname are the values from the php which stores the variables name.
$sql="INSERT INTO information(fname, lname, sex, add, e_address, n_years, f_song)
VALUES('$fname','$lname','$sex','$address','$email','$years','$favSong')";
//Check if insert successful.
if(mysqli_query($con, $sql)) {
header("Location:welcome.html");
mysqli_close($con);
}
else
{
echo "Error;".mysqli_error($con);
}
}//closing the "IF isset" statement.
//php closing.
?>
After looking at my code over and over i notice i was missing
Correct code
<?php
?>
instead I had
<?
?>

MySQL DB Column Not Updating after TinyMCE post INSERT

I've managed to implement a simple TinyMCE editor on my site and have it call using mySQL database. My problem Im having now is getting the DB to overwrite the contents already stored inside with what is being posted?
I simply want to overwrite the table contents (within the column) with what is being inserted.
Here is my code:
<!--- CONNECT TO THE DATABASE------>
<?php
$con = mysql_connect("localhost","root","jdkldk8%by");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cms", $con);
$sql="INSERT INTO tinymce (contents, contact, slider, resources)
VALUES
('$_POST[contents]','$_POST[contact]','$_POST[slider]','$_POST[resources]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
<!--- END DATABASE SETTINGS ----->
[ View your changes here ]
///////////////////////////////////////////////////////////////////////////////////////
Well... i tried again but its still not posting all fields to the DB! only updating 1.
<!--- CONNECT TO THE DATABASE------>
<?php
require_once('db.php');
$contents=$_POST['contents'];
$contact=$_POST['contact'];
$slider=$_POST['slider'];
$resources=$_POST['resources'];
$id='1';
$sql="UPDATE tinymce SET `contents`='$contents', `contact`='$contact', `slider`='$slider', `resources`='$resources' WHERE id='$id'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Saved!";
mysql_close($con);
?>
<!--- END DATABASE SETTINGS ----->
You want to use UPDATE, not INSERT, to update a previously created MySQL column.
Try editing a column using phpmyadmin and copying the resulting code from their MySQL code viewer.

issue when inserting data to database

My code doesn't insert any records to mysql. What is wrong? I am really confused.
I have designed a form and I want to read data from text box and send to the database.
<?php
if(isset($_post["tfname"]))
{
$id=$_post["tfid"];
$name=$_post["tfname"];
$family=$_post["tffamily"];
$mark=$_post["tfmark"];
$tel=$_post["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("university",$link);
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'$name','$family',$mark,$tel)";
mysql_query($insert,$link);
}
mysql_close($link);
?>
You'd better to put quotation mark for id, mark and tel after values in your query. Also as #Another Code said, you must use $_POST instead of $_post in your code. Try this and tell me the result:
<?php
if(isset($_POST["tfname"])) {
$id=$_POST["tfid"];
$name=$_POST["tfname"];
$family=$_POST["tffamily"];
$mark=$_POST["tfmark"];
$tel=$_POST["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link) {
die('Could not connect: ' . mysql_error());
} else {
mysql_select_db("university",$link);
$insert="insert into student
(sid,sname,sfamily,smark,stel) values
('$id','$name','$family','$mark','$tel')";
mysql_query($insert,$link) or die (mysql_error());
mysql_close($link);
}
} else {
die('tfname did not send');
}
?>
Use mysql_query($insert,$link) or die (mysql_error()); to fetch the error message.
With the code you've provided it could almost be anything - to do some tests... have you echo'd something to confirm you are even getting the tfname in POST? Does it select the database fine? Do the fields $id, $mark, and $tel need single quotes around them as well? We need to know more about where the code is not working to provide more help but that snippet appears as though it should be running, in the interim, please use some echo's to narrow down your problem!
Try to run the generated sql query in the sql query browser. Get the query by "echo $insert" statement.
change the $insert to:
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'".$name."','".$family."','".$mark."','".$tel."')";
further set ini_set('display_errors',1) so that php displays the error messages as required.
Lastly, whenever doing a mysql query, try to use or die(mysql_error()) in the query so that if somethings wrong with mysql or syntax, we are aware.
$q = mysql_query($query) or die(mysql_error());

Categories