Hi I have a code that print out some message if the data are successfully inserted into database.
This is the code:
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Record saved!";
How do I make it so that instead of opening a new page and print them, I need them to just display a message box indicating whether it is successful or not.
Thank you.
Try this:
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
echo '<script language="javascript">';
echo 'alert("Record saving failed")';
echo '</script>';
}else{
echo '<script language="javascript">';
echo 'alert("Record saved")';
echo '</script>';
}
if (!mysql_query($sql,$con)){
echo "<script type='text/javascript'>alert('failed!')</script>";
}else{
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
Use this to get a message box.
Related
My url type is like "http://localhost/boardgame/profile/Duncan/" the last word is the user name...
I just try to get some data from database and I need a user name to do this. I tried to get it from page URL but it doesn't work. Where did I go wrong? What has to be done? I'll be greateful if anybody can help...
<?php
$actual_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//echo $actual_link;
$rest = substr($actual_link, 35, -1);
echo $rest;
$link = mysqli_connect("localhost", "root", "", "bgt");
if (mysqli_connect_error()) {
die ("Veritabanı bağlantısında hata var");
} if (($rest) !="") {
if (get_current_user_id($link) == '0') {
echo "<p>Kullanıcının koleksiyonunu görebilmek için giriş yapmanız
gerekiyor.</p>";
} else {
$query = "SELECT TITLE FROM wp_user_collections WHERE `user_name` =
'".mysqli_real_escape_string($link, $rest)."'";
echo "<div><h1>'".mysqli_real_escape_string($link, $rest)."'in
Koleksiyonu</h1><div>";
if($result = mysqli_query($link, $query)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Oyun Adı</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>'" . $row['TITLE'] . "'</td>";
echo "</tr>";
}
echo "</table></div>";
}
}
}
} else {
echo "Kullanıcının koleksiyonunu görebilmek için <a
href='http://localhost/boardgame/profile/'".mysqli_real_escape_string($link,
$rest)."'>tıklayınız</a>";
}
?>
Here is the solution I have thought...using array then extracting the last element (or second last since there is a trailing '/' slash which is a delimiter)
$uri="http://localhost/boardgame/profile/Duncan/";
$uriParts=explode('/', $uri); //converts the uri string into an array of words separated by forward slash
$name=$uriParts[sizeOf($uriParts)-1-1]; //the user name comes from 2nd last item as the last item is empty
echo $name;
I could suggest you use Regular Expression to extract the user name, however I'm not well versed with it yet.
I need to create a div that only shows the information of the database, how should i connect the div to mysql so it only shows my database information?
I'm not sure I'm understanding your question... but I'll give it a try. The code below this will select whatever you tell it to from the database and echo it into a table.
<?php
$db = mysqli_connect('host', 'username', 'password', 'database');
$sql = "SELECT * FROM database"
mysqli_stmt_bind_param($db, "params here", your info here);
$res = mysqli_stmt_execute($sql);
mysqli_stmt_close($sql);
mysqli_close($db);
echo "<table>";
while($row = mysqli_fetch_array($res)){ //Creates a loop to loop through results
echo "<tr><td>" . $row[''] . "</td><td>" . $row[''] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
?>
I had created a table in mysql database. I am using php One column was for images, but it only stored images name. I wanted to print the whole table so did the coding but got got only the name of the images in the table.
Please explain how can I display images on the webpage that are stored in my PC. I don't want to save images in the database.
I used this code to print the table :
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$query="SELECT * FROM images LIMIT 0,5";
$result = #mysql_query($query);
echo "<table border='1'>
<tr>
<th>S.No.</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['image_id'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Hope you got some answer....
All your code is doing is sending the filename to the screen as a string. You need to the use image tags.
You may find this page useful, http://www.htmlcodetutorial.com/images/_IMG.html
I wan to always display the different pic. So there's a problem with looping the different pic. How do i do that?
index.php:
<?php
$result = mysql_query("SELECT * FROM table2", $connection );
echo '<ul>';
while($row = mysql_fetch_array($result)) {
if ($row)
echo "<li>";
echo "<img src='code.php?id=3'>";
echo " ";
echo "</p>";
echo "</li>";
echo "<br/>" ;
}
echo '</ul>';
mysql_close($connection);
?>
You need to pull the id value from the database. If you have a column called, maybe id you would want to put:
while($row = mysql_fetch_array($result)){
if ($row){
echo "<li>";
echo "<img src='code.php?id=".$row['id']."'/>"; // see what I did there?
echo " ";
echo "</p>"; // take this out
echo "</li>";
echo "<br/>"; // take this out
}
}
P.S. - don't write new code using the deprecated mysql extension. Use PDO or mysqli at least. And don't put <br />s between your <li>s, or close unopened <p>s. And, generally speaking, don't store your images in your database - just store the path to the image and put the images themselves in a folder on the server where they belong.
And please format your code - it is hard to read with no indentation or separation of files (your if statement was not properly enclosing what should have been conditioned statements). And mysql_real_escape_string is not as cool as you think it is.
Hope this helps.
I'm currently working on a project where I need to print out a lesson timetable. Here is a link to the one I have created http://conorhackett.com/tt/.
It works fine now but i'd like to have more control over it. If you look at the code you'll see what I mean. The html/css is very messy.
I've tried to do it with a html table but it didn't look great with all the lines.
Is there any foundations available to me that I could use as a base.?
Any adive greatly appreciated :)
--Conor
Update:
Hi Guys,
I've decided to go back to a html table. I am having extreme difficulty getting my head around the logic used to print the data.
Here is my code for printing the table:
foreach($bookingList->result_array() as $row)
{
$time = $row['start_time']. ' - ' .$row['end_time'];
$lesson = $row['lesson_type_name'];
$client = $row['client_firstname']. ' ' .$row['client_lastname'];
$horse = $row['horse_name'];
$fee = $row['fee'];
if(empty($prevLesson) && empty($prevTime))
{
echo '1';
$timeArr .= $time;
$lessonArr .= $lesson;
$clientArr .= $client;
$horseArr .= $horse;
$feeArr .= $fee.'-'.$i;
}
elseif($prevLesson == $lesson && $prevTime == $time)
{
echo '3';
echo '<br/>Previous: '.$prevTime.'++'.$prevLesson.'-'.$i.'<br/>';
echo '<br/>Current: '.$time.'++'.$lesson.'-'.$i.'<br/>';
$timeArr .= $time;
$lessonArr .= $lesson;
$clientArr .= $client;
$horseArr .= $horse;
$feeArr .= $fee.'-'.$i;
}
else
{
echo '3';
echo '<tr>';
echo '<td>(3)'. $timeArr .'</td>';
echo '<td>'. $lessonArr .'</td>';
echo '<td>'. $clientArr .'</td>';
echo '<td>'. $horseArr .'</td>';
echo '<td>'. $feeArr.'</td>';
echo '<td>'. $i.'</td>';
echo '</tr>';
$timeArr = ' ';
$lessonArr = ' ';
$clientArr = ' ';
$horseArr = ' ';
$feeArr = ' ';
$optionsArr = ' ';
$timeArr .= $time.'<br/>';
$lessonArr .= $lesson.'<br/>';
$clientArr .= $client.'<br/>';
$horseArr .= $horse.'<br/>';
$feeArr .= $fee.'-'.$i.'<br/>';
}
$prevTime = $time;
$prevLesson = $lesson;
$i += 1;
}
The idea for printing is:
Read data from DB and store in a string. when the data changes (time and lesson type) print the stored string, clear it and assign the new (different data) to the print string.
I have put up the code as is.. i'm just so frustrated and tired now. I have spent 3 hourse every evening this week trying to complete and have failed every time.. If you are willing to help me and need more detailed just let me now..
Any help really appreciated..
Thanks.
Give tables another shot, using this:
table {
border-collapse: collapse;
}
In addition to Amadan's suggestion, consider using colspan and rowspan to make each box take up the space required. Review the source code on these examples to see what I mean.
Soln: After the foreach loop had finished I still had data in the print string. I just echo that after the foreach loop and it solved everything. Thanks for the help input guys.