Jump to content

MNSarahG

Members
  • Posts

    17
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

MNSarahG's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. This (like all of these problems) is blowing my mind. I changed my connect statement to this: mysqli_connect("localhost","meetings","m33tings123","meetingsEventLocation") or die("<strong>Couldn't connect to database</strong>".mysqli_connect_error()); And now it seems to connect to the DB okay. Now my error is "Couldn't execute query." Not sure why. Thanks again for any advice!
  2. Hey, I pasted that code into the file and it still just comes back with "Unable to Select Database" (no extra info). - Sarah -
  3. Hi, I'm trying to run a search of table in my database. I have a page with this form: <form name="form" action="results.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search!" /> </form> That then uses this code to execute a search: <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } mysqli_connect("localhost","meetings","m33tings123"); mysqli_select_db("meetingsEventLocation") or die("Unable to select database"); // Build SQL Query $query = "select * from listings where name like \"%$trimmed%\" order by name"; $numresults=mysqli_query($query); $numrows=mysqli_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysqli_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysqli_fetch_array($result)) { $title = $row["1st_field"]; echo "$count.) $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>&nbsp "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> As of now, my error is "Unable to select database." I have triple checked my username, PW, database name, location, and all that, so I think it's coming from my attempt to change this code to work with MySQLi. Does anyone have any ideas? Thanks, Sarah
  4. BRILLIANT. And so simple. Thank you!!
  5. Hi, I'm trying to get this SQLi query to return entries that have the current month as their value in my database's "annMonth" field. The idea is that it returns a list of employees who have an anniversary in the current month. I'm having issues with (I think) my syntax, and a few hours of experimenting has yielded nothing. When I manually replace the variable in the WHERE clause with a "3" (instead of '$currDate;') it works fine. My code: $currDate = date("m"); /* Send a query to the server */ if ($result = mysqli_query($link, ' SELECT firstName, lastName, annMonth, annDay, annYear FROM employees WHERE annMonth = '$currDate;' ORDER BY annDay')) { Thanks in advance for any help! - Sarah -
  6. Sorry... I didn't explain properly (that totally did work though!). The employeeID is a number and in its own column in the DB (on the same table as firstName and lastName). The idea is that the user picks the name from the drop down, hits Delete, and the SQL query deletes the record based on the employeeID. So I need to carry that over to the action page somehow - that's why I was trying to get it to go through on the URL. Any ideas? Thanks again for the replies.
  7. Thanks so much for the replies! I'm really close... I followed Haku's suggestions and it almost works. My error message now is this: Here, the "German, Sarah" is the name that's on the drop-down from the 1st page that I selected to delete. What needs to happen is that the employeeID associated with that name makes it into the SQL DELETE instead of the name so that the delete action can work. Any suggestions?
  8. Also... here's the error I get: "Unable to Delete Record DELETE FROM employees WHERE employeeID= You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1" Because the script from the first page goes to this URL - deleteUser2.php?deleteUser=German%2C+Sarah - for a user with the first name Sarah and last name German. If I put in the URL I want it to go to - deleteUser2.php?employeeID=3 (in this case... the 3 is the variable) - the action works alright. Something's just off with my form.
  9. Here's the action page... <?php $hostname = 'localhost'; $username = 'zzz'; $password = 'zzz'; $mysqli = @new mysqli($hostname, $username, $password, 'greenspring'); if(!mysqli_connect_errno()) { $ID = $_GET['employeeID']; $sql = 'DELETE FROM employees WHERE employeeID='.$_GET["employeeID"].''; if($mysqli->query($sql) === TRUE) { echo 'Record Deleted successfully<br />'; } else { echo 'Unable to Delete Record<br />'.$sql.'<br />' . $mysqli->error; } $mysqli->close(); } else { echo 'Unable to connect'; exit(); } ?>
  10. Hi, I'm trying to get this Delete User form to work right, and I'm stuck on the part that should be easy. After connecting to the DB, I've got code that queries the database and outputs a list of names into a drop down that works, but what I want the form to do is go to another page with the Delete script (which also works) and carry the employeeID variable over in the URL to delete the right user. I think I'm close... if ($result = mysqli_query($link, 'SELECT employeeID, firstName, lastName FROM employees ORDER BY lastName')) { ?> <form name="deleteUser" action="deleteUser2.php?employeeID=<?php $_POST['employeeID'] ?> "> <select name="deleteUser"> <?php while( $row = mysqli_fetch_assoc($result) ) {printf("<option>%s, %s</option>\n", $row['lastName'], $row['firstName'], $row['employeeID']);} ?> </select> <br /><br /> <input type="submit" value="Delete" /> </form>
  11. I fixed part of it!! Everybody was probably really concerned. But I'm still struggling with getting the employee ID for each record to show up in the hyperlink. Again, help would be awesome. Here's what I've got for my query/output: if ($result = mysqli_query($link, 'SELECT employeeID, firstName, lastName FROM employees ORDER BY lastName')) { while( $row = mysqli_fetch_assoc($result) ){ printf("<a href=/profile.php?employeeID=$employeeID> %s, %s</a><br><br>\n", $row['lastName'], $row['firstName']); } Right now, each name links to "/profile.php?employeeID=" which is close... - Sarah -
  12. Hi, I'm using PHP5 with MySQLi, which has proven to be kind of a challenge for my first PHP project. I'm having a hard time finding good resources of example code and stuff. So if anybody knows of any good, beginner-friendly information out there, please share! That said, I'm having trouble with a seemingly simple issue and I can't really research my way out of it. I've outputted a list of names from my DB, and I want each name to be a hyperlink that carries an employee ID # over to another page. My script looks like this: <?php $link = mysqli_connect( 'localhost', 'user', 'pw', 'db'); if (!$link) { printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()); exit; } if ($result = mysqli_query($link, 'SELECT employeeID, firstName, lastName FROM employees ORDER BY lastName')) { ?> <a href=/profile.php?employeeID=$employeeID> <?php /* Fetch the results of the query */ while( $row = mysqli_fetch_assoc($result) ){ printf("%s, %s</a><br><br>\n", $row['lastName'], $row['firstName']); }?> <?php mysqli_free_result($result); } mysqli_close($link); ?> This outputs the list of names just fine, but only the first one is a link, and the employee ID variable doesn't make it into the link (just $employeeID). I think this is some sort of issue with my syntax. Thanks in advance for any help - I'm really liking PHP and am excited to be able to actually make things that work on my own! - Sarah -
  13. My DB looks like this in the query browser - the DB is called greenspring and the table I'm working with is employees. Nethnet... I tried running that and got a blank page.
  14. I got rid of the @ sign and double-checked my username/PW/database name. Those were all correct, and the script still didn't update the employees table. The account I'm using has permission to Select, Delete, Update, Insert, Create and Drop stuff in the DB. PHP and the MySQL are running on a machine here in my office (I'm working on a company intranet site). Again, not sure if it's relevant, but maybe it makes a difference. And thanks again.
  15. In response to the suggestions above... I ran this: <?php $username="xxx"; $password="xxx"; $database="xxx"; $firstName=$_POST['firstName']; $lastName=$_POST['lastName']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO employees (firstName,lastName) VALUES ('$firstName','$lastName')"; mysql_query($query) or die (mysql_error()); mysql_close(); ?> And - again - got a blank page and nothing new in the DB. No errors or anything. This is probably significant, but I just haven't got a clue what it means and am double-confused since my message board works. Any idea what to troubleshoot next? Thanks again guys.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.