|
$_POST and $_GET just for input type but my variable isn't that.this is my code I want when user click on button insert data to database but I can't access to variable
$row[lsname],$row[teachername],...even this page and other page?another my question is why variable like$_POST[t] isn't recognaize in function?!!!even I write global
$_POST[t] but it's error
<?php
session_start();
echo ($_SESSION[users]);
echo "$_POST[t]";
?>
<html>
<form method="post" action="reqire.php">
<?php
require_once ('db.php');
$db = new db("root","","regstudent","localhost");
function aa()
{
require_once ('db.php');
$db = new db("root","","regstudent","localhost");
$db->query("INSERT INTO register(`user`, `lsname`, `teachername`, `classday`, `starttime`, `endtime`, `examtime`)VALUES('$_SESSION[user]','$row[lsname]','$row[teachername]','$row[classday]','$row[starttime]','$row[endtime]','$row[examtime]') ");
echo "one record added";
}
echo "<table border='4' bordercolor='CC0099' width=100% height=20% >";
echo "<tr><th>Name</th><th>Professor</th><th>Capacity</th><th>Exam</th><th colspan='3'>classtime</th></tr>";
$result = mysql_query("SELECT * FROM class");
while($row = mysql_fetch_assoc($result))
{
if($_POST[t]==$row[lsname])
{
foreach($row as $key=>$var2)
{
echo "<td align='center'>$var2 </td>";
}
echo "<td align='center'><input type='submit' value='Register' onclick='aa()'></td> ";
}
echo "</tr>";
}
echo"</table>";
?>
</form>
</html>
please help me I mystify in my project :(
|
|
|
|
|
if you mean what I think you mean:
first page:
<?php
$sSomething='378923';
echo '<input type="hidden" name="something" value="'.htmlentities($sSomething).'">';
?>
second page:
<?php
$sSomething=(isset($_POST['something']) ? $_POST['something'] : '');
?>
|
|
|
|
|
When I must debug post, get or session variables I use this at the top of the target page.
<?php
session_start();
echo "<pre>";
print_r($_POST);
print_r($_SESSION);
echo "</pre>";
?>
This will allow you to see what is being passed to your target page so you will know if the data is making it or not.
I see that you defined your function aa()...but where is it called from?
Chris J
www.redash.org
|
|
|
|
|
I called it when I define submit button onclick attributes
echo "<td align='center'><input type='submit' value='Register' onclick='aa()'></td> ";
|
|
|
|
|
no you are calling a javascript function called aa() and that I'm guessing does not exist?
remeber php is executed on the server. The onclick is a javascript event that is triggered on the users browser. So you are most likely getting a undefined from the browser if the form submits to itself (i.e. - the scripts name is "reqire.php") But the error I think is wiped as the page reloads.
The php function aa() is never called.
On a side note, its really bad to:
1) have a database connection based on root, to much access for what is needed.
2) display a password in a forum, big security hole.
Chris J
www.redash.org
|
|
|
|
|
The function aa() work but I can't access the variable my big problem is I don't know user click in which button to insert data I agree with you but I can't insert in javascript!I think I should solve this problem in another way what's your suggestion?
|
|
|
|
|
This is what I have issues with in your code..
<?php
session_start();
echo ($_SESSION[users]);
echo "$_POST[t]";
?>
<html>
<form method="post" action="reqire.php">
<?php
require_once ('db.php');
$db = new db("root","","regstudent","localhost");
function aa()
{
require_once ('db.php');
$db = new db("root","","regstudent","localhost");
$db->query("INSERT INTO register(`user`, `lsname`, `teachername`, `classday`, `starttime`, `endtime`, `examtime`)VALUES('$_SESSION[user]','$row[lsname]','$row[teachername]','$row[classday]','$row[starttime]','$row[endtime]','$row[examtime]') ");
echo "one record added";
}
echo "<table border='4' bordercolor='CC0099' width=100% height=20% >";
echo "<tr><th>Name</th><th>Professor</th><th>Capacity</th><th>Exam</th><th colspan='3'>classtime</th></tr>";
$result = mysql_query("SELECT * FROM class");
$result = mysql_query("SELECT * FROM class") or die("<br>...LINE ".__LINE__."<br>...SQL: ".$sql."<br>...ERROR: ".mysql_error()."<br>");
while($row = mysql_fetch_assoc($result))
{
if($_POST[t]==$row[lsname])
{
foreach($row as $key=>$var2)
{
echo "<td align='center'>$var2 </td>";
}
echo "<td align='center'><input type='submit' value='Register' onclick='aa()'></td> ";
}
echo "</tr>";
}
echo"</table>";
?>
</form>
This is a framework of how I would structure the same thing...
<?php
session_start();
include("resources.php");
$dbh=dbconn();
if(isset($_POST['action']))
{
}
echo "<form action='reqire.php' method='post'>\n";
echo "</form>\n";
?>
Chris J
www.redash.org
|
|
|
|
|
Thanks alot for your help
|
|
|
|
|
To remove a file from php use the unlink() function. You can enter either the full path or the relative path to the file. You can see the examples below. All of them are valid.
unlink('deleteme.txt');
unlink('/home/arman/public_html/deleteme.txt');
unlink('./deleteme.txt');
unlink('../deleteme.txt');
PHP will give you a warning if the file you want to remove doesn't exist so you may want to force php to be quiet using the '@' character like this :
@unlink('./deleteme.txt');
Or you can first test if the file exist or not like thigs
$fileToRemove = '/home/arman/public_html/deleteme.txt';
if (file_exists($fileToRemove))) {
// yes the file does exist
unlink($fileToRemove);
} else {
// the file is not found, do something about it???
}
PHP will also give you a warning if you don't have enough permission to delete the file. It would be better if your code check if the file is removed successfully or not like show below
$fileToRemove = '/home/arman/public_html/deleteme.txt';
if (file_exists($fileToRemove)) {
// yes the file does exist
if (@unlink($fileToRemove) === true) {
// the file successfully removed
} else {
// something is wrong, we may not have enough permission
// to delete this file
}
} else {
// the file is not found, do something about it???
}
|
|
|
|
|
You should post those things in the tips/tricks forum...
Or even better you could make an article with a bunch of php tools...
|
|
|
|
|
Profile your code to pinpoint bottlenecks
Hoare's dictum states that Premature optimization is the root of all evil, an important thing to keep in mind when trying to make your web sites faster. Before changing your code, you'll need to determine what is causing it to be slow. You may go through this guide, and many others on optimizing PHP, when the issue might instead be database-related or network-related. By profiling your PHP code, you can try to pinpoint bottlenecks.
Upgrade your version of PHP
The team of developers who maintain the PHP engine have made a number of significant performance improvements over the years. If your web server is still running an older version, such as PHP 3 or PHP 4, you may want to investigate upgrading before you try to optimize your code.
Migrating from PHP 4 to PHP 5.0.x
Migrating from PHP 5.0.x to PHP 5.1.x
Migrating from PHP 5.1.x to PHP 5.2.x
Use caching
Making use of a caching module, such as Memcache, or a templating system which supports caching, such as Smarty, can help to improve the performance of your website by caching database results and rendered pages.
Use output buffering
PHP uses a memory buffer to store all of the data that your script tries to print. This buffer can make your pages seem slow, because your users have to wait for the buffer to fill up before it sends them any data. Fortunately, you can make some changes that will force PHP to flush the output buffers sooner, and more often, making your site feel faster to your users.
Output Buffering Control
Avoid writing naive setters and getters
When writing classes in PHP, you can save time and speed up your scripts by working with object properties directly, rather than writing naive setters and getters. In the following example, the dog class uses the setName() and getName() methods for accessing the name property.
class dog {
public $name = '';
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
Notice that setName() and getName() do nothing more than store and return the name property, respectively.
$rover = new dog();
$rover->setName('rover');
echo $rover->getName();
Setting and calling the name property directly can run up to 100% faster, as well as cutting down on development time.
$rover = new dog();
$rover->name = 'rover';
echo $rover->name;
Don't copy variables for no reason
Sometimes PHP novices attempt to make their code "cleaner" by copying predefined variables to variables with shorter names before working with them. What this actually results in is doubled memory consumption (when the variable is altered), and therefore, slow scripts. In the following example, if a user had inserted 512KB worth of characters into a textarea field. This implementation would result in nearly 1MB of memory being used.
$description = strip_tags($_POST['description']);
echo $description;
There's no reason to copy the variable above. You can simply do this operation inline and avoid the extra memory consumption:
echo strip_tags($_POST['description']);
Avoid doing SQL queries within a loop
A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts. In the example below, you can change the loop to build a single SQL query and insert all of your users at once.
foreach ($userList as $user) {
$query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
mysql_query($query);
}
Produces:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe")
Instead of using a loop, you can combine the data into a single database query.
$userData = array();
foreach ($userList as $user) {
$userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
}
$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData);
mysql_query($query);
Produces:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...
|
|
|
|
|
Author: Eric Higgins, Google Webmaster
Recommended experience: Beginner to intermediate PHP knowledge
PHP is a very popular scripting language, used on many popular sites across the web. In this article, we hope to help you to improve the performance of your PHP scripts with some changes that you can make very quickly and painlessly. Please keep in mind that your own performance gains may vary greatly, depending on which version of PHP you are running, your web server environment, and the complexity of your code.
|
|
|
|
|
Hello all,
Trying to get css working here...
Let's say I have a php file.
In that file I have something like:
if ($lang == "AAA"){
echo('<a href="'.$linkname.'" class="linkidiomaseleccionat">AAAA</a> | ');
}
else {
echo('<a href="'.$linkname.'" class="linkidioma">AAAA</a> | ');
}
Let's say the CSS file has this inside:
A.linkidiomaseleccionat, A.linkidiomaseleccionat:VISITED, A.linkidiomaseleccionat:ACTIVE, A.linkidiomaseleccionat:FOCUS, A.linkidiomaseleccionat:LINK{
color: #ff6d00;
text-decoration: none;
}
A.linkidiomaseleccionat:HOVER{
color: #ff6d00;
text-decoration: underline;
}
A.linkidioma, A.linkidioma:VISITED, A.linkidioma:ACTIVE, A.linkidioma:FOCUS, A.linkidioma:LINK{
color: #FFFFFF;
text-decoration: none;
}
A.linkidioma:HOVER{
color: #ff6d00;
text-decoration: underline;
}
A.linkNF, A.linkNF:VISITED, A.linkNF:ACTIVE, A.linkNF:FOCUS, A.linkNF:LINK{
color: #ff6d0N;
text-decoration: none;
}
A.linkNF:HOVER{
color: #ff6d0N;
text-decoration: underline;
}
A.linkNU, A.linkNU:VISITED, A.linkNU:ACTIVE, A.linkNU:FOCUS, A.linkNU:LINK{
color: #FFFFFF;
text-decoration: none;
}
A.linkNU:HOVER{
color: #ff6d0N;
text-decoration: underline;
}
Why on earth if I change the text that assigns the class to my links in the php file I don't get a good result if I'm not using "linkidiomaseleccionat" and "linkidioma"... I've tried to change the styule names to get something more consistent (bad initial design) and now I can't see it working if I change the name for it's counterparts: "linkNF" and "linkNU"...
Any of you could help me with that?
I'm feeling really bad here... I can't get it and I don't understand why changing only the name of the style class prevents it from working properly...
We... thank you in advance!
|
|
|
|
|
Hi Joan,
three comments:
1.
I don't really understand your question. It isn't clear to me what you want, what you get, and where they differ.
2.
you may suffer from cache effects; try CTRL/F5 to force the browser to reload the page and its dependencies (the CSS files).
3.
you probably could simplify by assigning more than one class to those anchor tags, as in:
echo('<a href="'.$linkname.'" class="linkidiomaseleccionat mySpecialColorScheme">AAAA</a> | ');
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hello all again,
Given that code:
<div id="IndexHeader">
<?php
echo('<div id="logoIndex"><img src="'.$path_images.'/logo.png" alt="alt text"></div>');
include($path_base.'/menu_idioma.php');
?>
</div>
If I'm echoing the path, copying it to the clipboard and then paste it into the explorer address bar, it seems right enough in order to allow my windows system to load the right image...
So... any idea on why is this happening? I'm really confused... those things should be extremely easy and they don't seem so...
As always thank you in advance...
|
|
|
|
|
would the image show if you hadn't the include statement?
it smells like file-not-found, possibly caused by your earlier include-changes-directory issue.
ADDED: does any of your code contain chdir() ?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
mmmm... strange...
If I look at the source code after the web page has been rendered I can see a perfect and correct path...
I've just put the two lines [echo + image] at the beggining of the index page with the same result.
No, I've not found one chdir in the entire site...
[here I'm crying/ranting]
Once again I'm calling (including) the web page from different sources and therefore the path from which it has been called changes and therefore the only way I've found to make everything work is to put complete paths to the files that I know where they are... Then putting "../" before one file is not enough and this is why I'm using this approach.
[just feeling a little bit better]
If I copy the path from the source code after the page has been rendered and then I paste the path into any explorer window the OS shows me the image correctly...
PS: Thank you very much for the patience...
|
|
|
|
|
I suggest you go to the bottom of this, it will haunt you forever if you don't.
I basically see two ways:
1.
install another PHP system and try your web site on it. I use XAMPP on my developing machines. Works great (PHP 5.2.5 + MySQL 5.0.51a)
2.
copy your entire web site and strip it down till you have the smallest one that exhibits the problem, then analyze closely.
Maybe first, if you haven't already, Google for known problems on your PHP system.
Good luck!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
More info:
It does not fail to find the image (I believe) in fact, when an image is not found then a red cross appears.
In this case the red cross is not there as in the place it should be I can see a small white page in which inside there is a red round, a green triangle... It is an image that the OS uses in some cases...
I'm even more confused now...
|
|
|
|
|
this could mean there are two file operations, one succeeding and one failing; maybe the first checks for file existence, the second actually reads the file (I wouldn't do it like that!).
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hello again:
If I try this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<div><img src="c:/aaa.png"></div>
</body>
</html>
It fails exactly in the same way...
But in that case it is IE9 who fails... I've only navigated to test.html (which has that content inside) and the result is the same I was telling you...
What I'm doing wrong?
|
|
|
|
|
that looks like a security precaution. Normally a web site is supposed to refer to its own pages, not to the local file system.
And AFAIK XAMPP and other PHP systems also have such (optional?) precautions; mine is set to allow access only to files that are under the server's root.
So what should work is file mypage.html containing
<html>
<body>
<img src="aaa.png">
<img src="images/bbb.png">
</body>
</html>
when it is stored at the site's root, together with aaa.png, and bbb.png in the subdirectory images.
You wouldn't like a web server to give access to files outside its root, would you? The entire file system would be up for grabs.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Working! thank you very much!
|
|
|
|
|
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
AAAARGH!
Of course it has worked well from the files that are in the root directory:
- root
-- images
-- templates
How do you solve the fact that there are files that are not in the root folder?
In my case I have index.php as a file that is being used as a template from multiple languages... then it happesn that it is being called (included) from different folders. Once this happens... then problems start...
index.php is at the root folder and it can access without trouble the images that are inside the "images" folder.
but any of the files that is being used inside any of the other folders i.e. index.php (when it is included from another folder) then I would need to use "../images/file.png".
How could I get the root of the web page folder? and then use it in order to load images?
Thank you in advance!
|
|
|
|
|