- This topic is empty.
-
AuthorPosts
-
April 29, 2014 at 11:14 pm #168908
dnyanesh
Participant**hi ,
i am new in this field. first time i am preparing webpage for practice where i want to add the data in to the database through webpage and want to display the same data on the webpage. **April 30, 2014 at 9:29 pm #168965__
Participant- Does your database exist yet? (do you know how to create one?)
- Does your database table exist yet? (does it have any records in it?)
- Do you have a connection to your database? (do you know how to connect to it?)
- Do you know how to query the database?
- Do you know how to retrieve the results?
Are you looking for a tutorial? you should try Google. The PHP Manual is also a great place to start: try the PDO extension.
Do you have any code? Show it to us, and explain your specific problem. Then we’ll be able to help.
edit:
Also, your title and post seem contradictory: are you trying to take data from the database and put it into the textarea, or from the textarea and put it into the database?
April 30, 2014 at 11:10 pm #168967dnyanesh
ParticipantFirst of all thanks a lot for your feedback .
i answer of the below question and i know it from W3schools.com and tutorialpoints . my query also resolved . …Does your database exist yet? (do you know how to create one?) Does your database table exist yet? (does it have any records in it?) Do you have a connection to your database? (do you know how to connect to it?) Do you know how to query the database? Do you know how to retrieve the results?
Since last week i started learning PHP , HTML and now i am trying to create web page where i can select,update , add and delete on web page it self. i could complete select and add . now i will start coding for update edit and delete.
please guide me to get more knowledge so that i can be perfect.
actually i was working in logistic company (KPO), but wanted to join IT so i left the previous job and now i am in my own field. everyday some or the other thing is new learning in this field . sorry to trouble you.
here is the code for add data in to the table .
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<meta name=”description” content=”” />
<meta name=”keywords” content=”” />
<meta name=”author” content=”” />
<link rel=”stylesheet” type=”text/css” href=”style.css” />
<title>First web page</title>
</head><body>
<?php include(‘includes/header.php’); ?>
<?php include(‘includes/nav.php’); ?>
<?php
$con = mysql_connect(“localhost”,”root”,””);
mysql_select_db(“firstweb”);/*if(isset($con))
{
echo “successfullu”;
}else{ echo mysql_error();}*/
if(isset($_POST[‘sub’]))
{
//$sr_no=$_POST[‘sr_no’];$name=$_POST[‘name’];
$email=$_POST[’email’];
$date=$_POST[‘date’];
$mobile=$_POST[‘mobile’];
$gender=$_POST[‘gender’];
$lang=$_POST[‘lang’];$sql=”insert into emp_data values(”,’$name’,’$email’,’$date’,’$mobile’, ‘$gender’,’$lang’)”;
$rec_insert = mysql_query($sql);
if($rec_insert)
{
echo “entered data succesfully”;
}
else
{
echo “entered data not succesfully”.mysql_error();}
}
?>
<h3>Paragraph Element</h3><form action=”” method=”post”>
<table width=”400″ height=”109″ border=”1px solid black” >
<tr><td width=”63″>Name</td>
<td width=”169″>E-mail</td>
<td width=”85″>DOB</td>
<td width=”104″>Mobile_No</td>
<td width=”50″>Gender</td>
<td width=”69″>Language</td>
</tr>
<tr id=”bgcolor”><td class=”back”><input type=”text” name=”name” /></td>
<td class=”back”><input type=”text” name=”email” /></td>
<td class=”back”><input type=”date” name=”date” /></td>
<td class=”back”><input type=”text” name=”mobile” /></td>
<td class=”back”><input id=”bgcolor” type=”radio” name=”gender” value=”M” checked=”checked”>M</input> <input type=”radio” name=”gender” value=”F”>F</input></td>
<td class=”back”><select name=”lang”><option value=”English” >English</option>
<option value=”Marathi”>Marathi</option>
<option value=”Hindi”>Hindi</option> </select></td></tr>
<tr>
<td colspan=”7″><input type=”submit” value=”Submit” name=”sub” /> <input type=”button” value=”Back”/> </td>
</tr>
</table>
</form><?php include(‘includes/sidebar.php’); ?>
<?php include(‘includes/footer.php’); ?>
</body>
</html>
May 2, 2014 at 7:27 am #169067__
Participant@TheDoc (or another Mod) —I have a post in this thread that disappeared sometime last night. Any help getting it back? I appreciate it!
May 2, 2014 at 7:34 am #169069Paulie_D
MemberCodedumps are the worst!
May 2, 2014 at 10:05 am #169086__
ParticipantRebuilding my previous reply (thanks for trying, @Paulie_D):
i know it from W3schools.com
- Don’t learn from w3schools.
Use MDN or sitepoint. PHP.net is the #1 place for php issues. Good SQL sites are harder to find, but you might try SQLZOO.net, SQLCourse.com, or FirstSQL.com. And SQLfiddle.com is great for testing your queries.
$con = mysql_connect(“localhost”,”root”,””);
- Do not learn the
mysql_*
functions.
They are deprecated and have been outdated for over ten years. They are inefficient, do not support modern MySQL features, and make security difficult.
Learn PDO or MySQLi instead.
$name=$_POST['name'];
…
$sql=”insert into emp_data values(”,’$name’
…- Never put user-supplied data directly into an SQL statement. This creates a security risk called SQL Injection.
Remember, “Never Trust User Input.” Always assume your user is going to either (a) make a mistake, or (b) attack your website. Validate all user input (make sure it is the data it is supposed to be), and Sanitize it (make it safe to use in SQL) before using it.
Both PDO and MySQLi support prepared statements, which can completely prevent SQL Injection attacks.
Also, you should always explicitly list column names in queries. Using “
*
” is okay for dev/testing, but your finished code should list each column you use by name (even if you’re using _all_ of them). This makes mistakes harder and maintenance easier.<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
There’s no reason (at all) to use older, confusing DTDs. The HTML5 version triggers standards mode as well as (or better) than any other html doctype can, and is much easier to type:
<!doctype html>
If you want to continue using the xhtml serialization, you can, though -again- there is really no reason to (in fact, unless you’ve gone to significant lengths, browsers are going to treat it as “broken html” and not as xml anyway).
When you write PHP code, you can sort it into two basic categories: (1) program logic and processing (e.g., conditional statements, database queries, manipulating info), and (2) templating code (i.e., anything that produces output to the browser).
If your PHP is nothing but templating, that’s fine; but when you start adding actual programming logic into it, you should make sure all of that logic goes first, and all of the output goes last.
For example, you start your php script by outputting HTML markup. Later, you query your database. What if there’s an error? You can’t “take back” or “rewrite” the HTML you’ve already shown to the user. You won’t be able to recover from the error; and the user will be stuck with a broken page.
Codedumps are the worst!
Agreed. Small bits of code are fine on the forums (but use the
[Inline Code]
or[Block Code]
formatting buttons!), but larger amounts of code are really hard to read here on css-tricks. Use an online service like pastebin, or make a gist on github.May 2, 2014 at 10:09 am #169087__
ParticipantLearn PDO or MySQLi instead.
Links:
Also, one thing to keep in mind while you’re learning: SQL is not part of PHP. It is its own program and its own programming language. All you’re doing in PHP is writing SQL Statements that your database will execute. So, if you have problems, make sure your statements work first, and then try to put them into PHP.
May 5, 2014 at 5:08 am #169234dnyanesh
ParticipantThanks you traq.
I need these SQL statements in PHP only. i had issue related to insert data in to database and again show it in table. i did it with the help of your support. i need your support for update and delete coding . i want to update the record in the table from web page . means i will click on update in from of row and it should redirect to update page and required field should get update .here is the code for update
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta name="author" content="" /> <link rel="stylesheet" type="text/css" href="style.css" /> <title>First web page</title> </head> <body> <div> <?php include('includes/header.php'); ?> <?php include('includes/nav.php'); ?> <div> <h3>Paragraph Element</h3> <?php function renderForm($id, $name, $email, $date,$mobile,$gender,$lang,$error) { ?> <form action="" method="post" enctype="multipart/form-data"> <table class="tbldata" align="center" border="1px solid black" style="background-color:!important" > <tr id="bgcolor"> <td colspan="2" align="center" style="background-color: #CF3">Submit Employee Information</td> </tr> <tr id="bgcolor"> <td >Name</td> <td class="back"><input type="text" name="name" value="<?php echo $name ?>"/></td> </tr> <tr id="bgcolor"> <td>E-mail</td> <td class="back"><input type="text" name="email" value="<?php echo $email ?>" /></td> </tr> <tr id="bgcolor"> <td>DOB</td> <td class="back"><input type="date" name="date" value="<?php echo $date ?>" /></td> </tr> <tr id="bgcolor"> <td>Mobile_No</td> <td class="back"><input type="text" name="mobile" value="<?php echo $mobile ?>"/></td> </tr> <tr id="bgcolor"> <td>Gender</td> <td class="back"><input id="bgcolor" type="radio" name="gender" value="<?php echo $gender ?>" checked="checked">M</input> <input type="radio" name="gender" value="<?php echo $gender ?>">F</input></td> </tr> <tr id="bgcolor"> <td>Language</td> <td class="back"><select name="lang"><option value="<?php echo $lang ?>" >English</option> <option value="<?php echo $data->lang ?>">Marathi</option> <option value="<?php echo $data->lang ?>">Hindi</option> </select></td> </tr> <tr id="bgcolor"> <td>Photo</td> <td class="back"><input type="file" name="file" value="<?php echo $file ?>" /></td> </tr> <tr> <td colspan="7"><input type="submit" value="Submit" name="update" /> <a href="dashboard.php"> <input type="button" value="Back"/></a> </td> </tr> </table> </form> <?php } ?> </div> <?php $con = mysql_connect("localhost","root",""); mysql_select_db("firstweb"); if (isset($_POST['update'])) { if (is_numeric($_POST['id'])) { $id = $_POST['id']; $name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); $email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); $date = mysql_real_escape_string(htmlspecialchars($_POST['date'])); $mobile = mysql_real_escape_string(htmlspecialchars($_POST['mobile'])); $gender = mysql_real_escape_string(htmlspecialchars($_POST['gender'])); $lang = mysql_real_escape_string(htmlspecialchars($_POST['lang'])); if ($name == '' || $email == '' || date == '' || mobile == '' ) { $error = 'ERROR: Please fill in all required fields!'; renderForm($id, $name, $email, $date,$mobile,$gender,$lang,$error); } else { mysql_query("UPDATE emp_data SET name='$name',email='$email',date='$date',mobile='$mobile',gender='$gender',lang='$lang',image='$image' where id='$id'") or die(mysql_error()); header("Location: dashboard.php"); } } else { echo 'Error!'; } } else { if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) { $id = $_GET['id']; $result = mysql_query("SELECT * FROM emp_data WHERE id=$id") or die(mysql_error()); $row = mysql_fetch_array($result); if($row) { $name = $row['name']; $email = $row['email']; $date = $row['date']; $mobile = $row['mobile']; $gender = $row['gender']; $lang = $row['lang']; renderForm($id, $name, $email, $date,$mobile,$gender,$lang,''); } else { echo "No results!"; } } else { echo 'Error!'; } } ?> <?php include('includes/sidebar.php'); ?> <?php include('includes/footer.php'); ?> </div> </body> </html>
May 5, 2014 at 10:37 am #169241__
ParticipantI need these SQL statements in PHP only
Well, I understand that you’re using php to access the database. But my point above was that SQL is used by the database: php does not “use” SQL statements at all. As far as PHP is concerned, it is not “code” at all; it’s just plain text.
That’s why it is so useful, when you’re having a problem with an SQL statement, to actually
echo
the final statement out and test it directly in the database (or using a tool like sqlfiddle.com).i need your support for update and delete coding . i want to update the record in the table from web page . means i will click on update in from of row and it should redirect to update page and required field should get update .
I understand your goals, and I would be happy to help you.
I do not understand what your actual question is.You need to be specific. What is actually going wrong? What happens, compared to what you expect? “Not working” is not a helpful thing to say. It is so vague as to be basically meaningless (it could mean almost anything; from “forgot a semicolon” to “didn’t plug in the computer”).
Are you getting error messages?
(do you have error reporting enabled?)Again, if the problem is in your SQL statement, it will be much easier to troubleshoot from MySQL than from PHP.
~~~~~~~~~~
Another bit of advice, looking at your code: don’t use
htmlspecialchars
when you insert data into the database.mysql_real_escape_string
is the appropriate* function here.htmlspecialchars
should be used when you display text in HTML.* again, however, you should not use the
mysql_
functions at all.Working with ext/mysql because you have an existing codebase is one thing; but you are learning and writing new code. Use PDO or MySQLi instead.
May 5, 2014 at 11:37 pm #169293dnyanesh
Participanthey Traq Good morning,
Thanks for reply…
yesterday i made some changes in update code now it is working…. sorry i am troubling you.
i have started MYSQLi . but the problem my all colleagues are using mysql and as i am new. i may ask you some fullish questions while learning.
instead of mysql in the above code if i use mysqli . Do i need to config any file or else i can use it directly as per the syntax given in the manual
<?php
$con = mysql_connect(“localhost”,”root”,””);
mysql_select_db(“firstweb”);
$id =$_GET[‘id’];
if (isset($_POST[“update”]))
{
$name = mysql_real_escape_string(htmlspecialchars($_POST[“name”]));
$email = mysql_real_escape_string(htmlspecialchars($_POST[“email”]));
$date = mysql_real_escape_string(htmlspecialchars($_POST[“date”]));
$mobile = mysql_real_escape_string(htmlspecialchars($_POST[“mobile”]));
$gender = mysql_real_escape_string(htmlspecialchars($_POST[“gender”]));
$lang = mysql_real_escape_string(htmlspecialchars($_POST[“lang”]));
//$photo =($_POST[“photo”]);
$photo=$_FILES[‘file’][‘name’];
move_uploaded_file($_FILES[‘file’][‘tmp_name’],”targetfolder/”.$photo);
//$id = mysql_real_escape_string(htmlspecialchars($_POST[“id”]));mysql_query("UPDATE emp_data SET name='$name',email='$email',date='$date',mobile='$mobile',gender='$gender',lang='$lang',photo='$photo' where id='$id'"); } $result = mysql_query("SELECT * FROM emp_data WHERE id='$id'"); $row =mysql_fetch_array($result); ?>
Guide me on this….
May 6, 2014 at 10:14 am #169337__
Participanti have started MYSQLi . but the problem my all colleagues are using mysql and as i am new. i may ask you some fullish questions while learning.
No problem. And if your job requires you to learn the
mysql_
functions, then, of course, you should learn them. Even if that is the case, however, you’ll definitely be better off learning mysqli/pdo also. : )i use mysqli . Do i need to config any file or else i can use it directly as per the syntax given in the manual
You may put your database credentials (host, username, etc.) in your php.ini file, but no: you are not required to. You may provide the credentials in your code like normal.
MySQLi has a very similar process to the old mysql_ functions. In addition to having individual functions, it also supports objects. Personally, I find objects much easier to work with, though they are a change from functional programming. For example, instead of something like this:
// connect $db = mysqli_connect( 'host','username','password','db_name' ); if( ! $db ){ /* error: unable to connect */ } // write your sql with _placeholders_ $sql = "select something from my_table where this=?"; // make a _prepared statement_ from your sql $stmt = mysqli_prepare( $db,$sql ); // this attaches the data that goes with the query // (in place of the "?"), // but mysql will know it is _data_, so there is no security risk mysqli_stmt_bind_param( $stmt,'s',$_GET['this'] ); // execute (send) the statement mysql_stmt_execute( $stmt ); // get the results mysqli_stmt_bind_results( $stmt,$something ); mysqli_stmt_fetch( $stmt ); // $something now holds the value from the DB echo $something;
You could use the object oriented way:
// connect $db = new mysqli( 'host','username','password','db_name ); if( $db->connect_errno ){ /* error: unable to connect */ } // prepare your statement $sql = "select something from my_table where this=?"; $stmt = $db->prepare( $sql ); // bind your data to the parameters ("?") $stmt->bind_param( 's',$_GET['this'] ); // execute (send) the statement $stmt->execute(); // get the results $stmt->bind_result( $something ); $stmt->fetch(); echo $something;
I prefer using PDO to MySQLi (it streamlines parts of this process, and you can use named parameters (like
this=:this
instead ofthis=?
)), but PDO has no functions (it’s all objects, like my second example above). If you are willing to work with objects, I’d recommend PDO instead of MySQLi; but I can help you with either.May 7, 2014 at 12:33 am #169395dnyanesh
ParticipantThank you.
May 8, 2014 at 12:44 am #169525dnyanesh
ParticipantHey Traq ,
Good morning..
i need your help for cke editor . i am trying to include ckeditor in HTML code but it is not working. i have included ckeditor.js files in the file.i want replace textarea with ckeditor but its not working . please help me out
here is the code .<!DOCTYPE html>
<html>
<head>
<title>A Simple Page with CKEditor</title><script src="ckeditor.js" ></script> </head> <body> <form> <textarea name="editor1" id="editor1" rows="10" cols="80"> This is my textarea to be replaced with CKEditor. </textarea> <script> CKEDITOR.replace( 'editor1' ); </script> </form> </body>
</html>
May 8, 2014 at 1:06 am #169527__
ParticipantMay 9, 2014 at 10:08 am #169597dnyanesh
Participanti want to update selected rows when i will click on submit button . here check box is not getting value . kindly help me out
<!DOCTYPE html>
<html>
<head>
<title>Dhanaji Nana Mahavidyalaya</title>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
</head><body class=”body”>
<?php
session_start();
?>
<form action=”” method=”post”><h2><br>
Welcome <?php echo $_SESSION[“username”]; ?></h2><h3 align=”center” style=”color:#F00″>Login Here</h3>
<h6>Student Login » Parents Login » Admin Login </h6><?php include(‘includes/nav.php’); ?>
<legend>Student Information</legend><table style=”border-collapse:collapse; “width=”589″ border=”1”>
<tr>
<td>Select</td>
<td>Name</td>
<td>Degree</td>
<td>Birthdate</td>
<td>Family Income</td>
<td>Father Name</td>
<td>Roll No</td>
<td>Update</td>
<td>Delete</td>
<?php // <td>Dept Name</td>?>
</tr>
<?php$con = mysqli_connect(“localhost”,”root”,””,”collage”);
mysqli_select_db($con , “collage”);//$sql =”select * from stud_info”;
$sql = mysqli_query($con , “SELECT * FROM stud_info”);
//$result = mysql_query($sql);$count = mysqli_num_rows($sql);
while($rows = mysqli_fetch_array($sql, MYSQLI_ASSOC ))
{
?><tr>
<td ><input type=”hidden” name=”checkboxid[]” value=”<? echo $rows[‘stud_id’]; ?>” /><? echo $rows[‘stud_id’]; ?></td><td><input type=”checkbox” name=”onoffcheckbox[]” id=”onoff” value=”<?php echo $rows[‘stud_id’]; ?>”/></td>
<td><input type=”text” name=”name<?php $rows[‘stud_id’]; ?>” id=”name” value=”<?php echo $rows[‘name’]; ?>” /></td>
<td><input type=”text” name=”Degree<?php $rows[‘stud_id’]; ?>” id=”Degree” value=”<?php echo $rows[‘Degree’]; ?>” /></td>
<td><input type=”text” name=”birthdate<?php $rows[‘stud_id’]; ?>” id=”birthdate” value=”<?php echo $rows[‘birthdate’]; ?>” /></td>
<td><input type=”text” name=”income<?php $rows[‘stud_id’]; ?>” id=”income” value=”<?php echo $rows[‘income’]; ?>” /></td>
<td><input type=”text” name=”fathername<?php $rows[‘stud_id’]; ?>” id=”fathername” value=”<?php echo $rows[‘fathername’]; ?>” /></td>
<td><input type=”text” name=”roll_no<?php $rows[‘stud_id’]; ?>” id=”roll_no” value=”<?php echo $rows[‘roll_no’]; ?>” /></td><td>“> Edit </td>
<td>“>Delete</td>
</tr>
<?php } ?><tr>
<td colspan=”9″><input type=”button” value=”Add Student information” /></td></tr><tr>
<td colspan=”9″>” ><input type=”button” value=”Add”/> <input type=”submit” value=”Update”/><input type=”submit” value=”Delete”/></td>
</tr>
</table>
<?php
if($_POST)
{
echo ”"; print_r($_POST); die('svsdfsf');
foreach($_POST[‘stud_id’] as $id )
{
$sql1=”UPDATE “.$stud_info.” SET onoff='”.$_POST[“onoff”.$id].”‘, name='”.$_POST[“name”.$id].”‘, Degree='”.$_POST[“Degree”.$id].”‘, birthdate='”.$_POST[“birthdate”.$id].”‘,income='”.$_POST[“income”.$id].”‘,fathername='”.$_POST[“fathername”.$id].”‘,roll_no='”.$_POST[“roll_no”.$id].”‘ WHERE stud_id='”.$id.”‘”;
$result1 = mysql_query($sql1);
}
}
if($result1)
header(“location:stud_info.php”);?>
</fieldset>
</form>
</body>
</html> -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.